Due: Friday Points: 35 Program 1 – (15 points) Create a method called GreatestCo
ID: 3909822 • Letter: D
Question
Due: Friday
Points: 35
Program 1 – (15 points)
Create a method called GreatestCommonDivisor that uses Euclid’s algorithm as defined in this video
https://www.youtube.com/watch?v=fwuj4yzoX1o. Do not search for C# versions of the algorithm on the Web or in textbooks –everything you need is explained in the video and there should be no need for any additional “research.”
Your method signature should look like this:
int GreatestCommonDivisor(int a, int b)
Create a test program that demonstrates the following
GreatestCommonDivisor(164, 410) = 82
GreatestCommonDivisor(87801, 1469) = 113
Program 2 – (20 points)
Create a fractions class that represents fractions in the form a/b Your class should implement the following members:
int Numerator
int Denominator
Fraction(int numerator, int denominator) creates a new Fraction
double ToDecimal() returns the fraction as a double
Fraction Add(Fraction f) adds the fraction to the one passed in and simplifies the result
Fraction Multiply(Fraction f) multiplies the fraction by the one passed in and simplifies the result
Fraction Simplify() simplifies the fraction using the GreatestCommonDivisor method from the first Exercise
Create a test program that demonstrates the following:
1/2 = 0.5
1/7 + 1/5 = 12/35
1/4 * 2/3 * 4/5 = 2/15
Ineed this in C# not C++ so can any one help me out with this please.
Explanation / Answer
Hello Sir/Madam
Here is the C# program to find GCD using euclids algorithm
using System;
public class Program
{
static int GreatestCommonDivisor(int num1, int num2)
{
int Remainder;
while (num2 != 0)
{
Remainder = num1 % num2;
num1 = num2;
num2 = Remainder;
}
return num1;
}
static int Main(string[] args)
{
int x, y;
Console.Write("Enter the First Number : ");
x = int.Parse(Console.ReadLine());
Console.Write("Enter the Second Number : ");
y = int.Parse(Console.ReadLine());
Console.Write(" The Greatest Common Divisor of ");
Console.WriteLine("{0} and {1} is {2}", x, y, GreatestCommonDivisor(x, y));
Console.ReadLine();
return 0;
}
}
Here is the another program
#include <iostream>
using namespace std;
class Fraction
{
private:
int numerator, denominator;
public:
Fraction()
{
numerator = 1;
denominator = 1;
}
Fraction(int n, int d)
{
numerator = n;
if (d==0)
{
cout << "ERROR: ATTEMPTING TO DIVIDE BY ZERO" << endl;
exit(0); // will terminate the program if division by 0 is attempted
}
else
denominator = d;
}
Fraction Sum(Fraction otherFraction)
{
int n = numerator*otherFraction.denominator+otherFraction.numerator*denominator;
int d = denominator*otherFraction.denominator;
return Fraction(n/gcd(n,d),d/gcd(n,d));
}
Fraction Difference(Fraction otherFraction)
{
int n = numerator*otherFraction.denominator-otherFraction.numerator*denominator;
int d = denominator*otherFraction.denominator;
return Fraction(n/gcd(n,d),d/gcd(n,d));
}
Fraction Product(Fraction otherFraction)
{
int n = numerator*otherFraction.numerator;
int d = denominator*otherFraction.denominator;
return Fraction(n/gcd(n,d),d/gcd(n,d));
}
Fraction Division(Fraction otherFraction)
{
int n = numerator*otherFraction.denominator;
int d = denominator*otherFraction.numerator;
return Fraction(n/gcd(n,d),d/gcd(n,d));
}
int gcd(int n, int d)
{
int remainder;
while (d != 0)
{
remainder = n % d;
n = d;
d = remainder;
}
return n;
}
void show() // Display method
{
if (denominator == 1) // e.g. fraction 2/1 will display simply as 2
cout << numerator << endl;
else
cout << numerator << "/" << denominator << endl;
}
};
int main()
{
Fraction a(1,2);
Fraction b(1,4);
Fraction c;
c = a.Sum(b); // Result: 3/4
c.show();
c = a.Product(b); // Result: 1/8
c.show();
c = a.Division(b); // Result: 2
c.show();
return 0;
}
Run the above programs give the above inputs and u can obtain the results
Hope It works and looking forward to help if have any doubts..
Thank You Sir/Madam.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.