Enter the First Number : 12 Enter the Second Number : 24 The Greatest Common Divisor of 12 and 24 is : 12
This is a C# Program to perform gcd.
This C# Program Performs GCD.
Greatest Common Divisor of two numbers is the largest positive numbers which can divide both numbers without any remainder
Here is source code of the C# Program that calculate GCD between two numbers. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.
/* * C# Program to Perform GCD */ using System; public class Program { static int GCD(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("\nThe Greatest Common Divisor of "); Console.WriteLine("{0} and {1} is {2}", x, y, GCD(x, y)); Console.ReadLine(); return 0; } }
This C# program is used to perform GCD. We are entering the first and second numbers using an x and y variables. Using while loop we are computing the GCD, is a Greatest Common Divisor of two numbers is the largest positive numbers which can divide both numbers without any remainder.
We are checking that ‘num2’ variable value is not equal to 0. If the condition is true, then we are executing the statement and computing the modulus of ‘num1’ variable value by ‘num2’ variable values and assigns to ‘remainder’ variable. Then we are assigning the ‘num2’ variable value to ‘num1’ variable value and ‘remainder’ variable value to ‘num2’ variable. Hence we are displaying the output of the program.
Enter the First Number : 12 Enter the Second Number : 24 The Greatest Common Divisor of 12 and 24 is : 12