Simplification of fractions

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace Simplification

{

    class Program

    {

        static void Main(string[] args)

        {

            //Program to simplify a fraction

            Console.Write("Enter numerator: ");

            int num = int.Parse(Console.ReadLine());

            Console.Write("Enter denominator: ");

            int den = int.Parse(Console.ReadLine());

            while (hcf(num, den) != 1)

            {

                int div = hcf(num, den);

                num = num / div;

                den = den / div;

            }

            Console.WriteLine("Simplified Fraction: ");

            Console.WriteLine(num + "/" + den);

        }

        public static int hcf(int num1, int num2)

        {

            int min = Math.Min(num1, num2);

            int hcf = 1;

            for (int i = 2; i <= min; i++)

            {

                if (num1 % i == 0 && num2 % i == 0)

                {

                    hcf = i;

                }

            }

            return hcf;

        }

    }

}

 

Enter numerator: 12
Enter denominator: 9
Simplified Fraction:
4/3
Press any key to continue . . .

Advertisements
This entry was posted in Information Technology. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s