In C not C 1 Create a method called GreatestCommonDivisor th

In C#, not C++

1. Create a method called GreatestCommonDivisor that used Euclids algorithm as defined in this video https://www.youtube.com/watch?v=fwuj4yzoX1o - do not search out c# versions of the algorithm, 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

2. 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

Solution

1

using System;

public class Test
{
   public static void Main()
   {
         int num1, num2;
         //Input two numbers

        Console.Write(\"Enter the First Number : \");

        num1 = Convert.ToInt32(Console.ReadLine());

        Console.Write(\"Enter the Second Number : \");

        num2 = Convert.ToInt32(Console.ReadLine());

        Console.Write(\"\ The Greatest Common Divisor of \");

        Console.WriteLine(\"{0} and {1} is {2}\", num1,num2, GreatestCommonDivisor(num1,num2)); //call to function

        Console.ReadLine();
   }
   public static int GreatestCommonDivisor(int a, int b)
   {
                int remainder;

        while (b != 0)

        {

            remainder = a % b;

            a = b;

            b = remainder;

        }

        return a;
   }
}


Output:

Enter the First Number : 164 Enter the Second Number : 410

The Greatest Common Divisor of 164 and 410 is 82

2

using System;

class Fraction
{
    private int Numerator;
    private int Denominator;
  
    public Fraction()
    {
        Numerator = 0;
        Denominator = 1;
    }
  
    public Fraction(int numerator, int denominator) // creates a new Fraction
    {
        Numerator = numerator;
        Denominator = denominator;
    }
    public double ToDecimal() //returns the fraction as a double
    {
        return (double)Numerator/Denominator;
    }
public Fraction Add(Fraction f) // adds the fraction to the one passed in and simplifies the result
{

    Numerator = Numerator*f.Denominator + f.Numerator*Denominator;
    Denominator = Denominator*f.Denominator;
    this.Simplify();
    return this;
}
public Fraction Multiply(Fraction f) // multiplies the fraction by the one passed in and simplifies the result
{
  
    Numerator = Numerator*f.Numerator;
    Denominator = Denominator*f.Denominator;
    this.Simplify();
    return this;
}

public Fraction Simplify() //simplifies the fraction using the GreatestCommonDivisor method
{

int remainder;
int d = Denominator;
int n = Numerator;

while (d != 0)

        {

            remainder = n % d;

            n = d;

            d = remainder;

        }

Numerator = Numerator/n;
Denominator = Denominator/n;
return this;
  
}
public override string ToString()
{
    return \"Numerator : \"+Numerator+\" Denominator : \"+Denominator;
}
  
  
}

public class Test
{
   public static void Main()
   {
         int num, den;
       
     Fraction f1 = new Fraction(1,2);
   
     Console.WriteLine(f1.ToDecimal());
   
     Fraction f2 = new Fraction(1,7);
   
     Fraction f3 = new Fraction(1,5);
   
     Console.WriteLine(f2.Add(f3).ToString());
   
     Fraction f4 = new Fraction(1,4);
   
     Fraction f5 = new Fraction(2,3);
   
     Fraction f6 = new Fraction(4,5);
   
     Console.WriteLine(f4.Multiply(f5).ToString());
   
      
   }
  
              
}


Output:

0.5

Numerator : 12 Denominator : 35

Numerator : 1 Denominator : 6

In C#, not C++ 1. Create a method called GreatestCommonDivisor that used Euclids algorithm as defined in this video https://www.youtube.com/watch?v=fwuj4yzoX1o
In C#, not C++ 1. Create a method called GreatestCommonDivisor that used Euclids algorithm as defined in this video https://www.youtube.com/watch?v=fwuj4yzoX1o
In C#, not C++ 1. Create a method called GreatestCommonDivisor that used Euclids algorithm as defined in this video https://www.youtube.com/watch?v=fwuj4yzoX1o
In C#, not C++ 1. Create a method called GreatestCommonDivisor that used Euclids algorithm as defined in this video https://www.youtube.com/watch?v=fwuj4yzoX1o

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site