C# Program to Find GCD of two Numbers

In this C# program, we will take the input from the user and find GCD of two numbers.

C# program to Find GCD of two Numbers Code:

 private static void Main(string[] args)
        {
            int n1, n2, i, gcd = 0;

            Console.Write("Enter first number : ");
            n1 = Convert.ToInt32(Console.ReadLine());

            Console.Write("Enter second number : ");
            n2 = Convert.ToInt32(Console.ReadLine());

            for (i = 1; i <= n1 && i <= n2; ++i)
            {
                if (n1 % i == 0 && n2 % i == 0)
                gcd = i;
            }

            Console.WriteLine("G.C.D of {0} and {1} is {2}", n1, n2, gcd);
            Console.ReadLine();
        }

C# program to Find GCD of two Numbers Output:

CSharp-program-to-Find-GCD-of-two-Numbers

 
Best quality Asp .Net Ajax Control Toolkit tutorials.

Give your valuable comments.

Name
Email
Comment
5 + 7 =
 

About Us | Terms of Use | Privacy Policy | Disclaimer | Contact Us Copyright © 2012-2024 CodingFusion
50+ C# Programs for beginners to practice