# C# Program to Find GCD of Two Numbers

This is a C# Program to perform gcd.

Problem Description

This C# Program Performs GCD.

Problem Solution

Greatest Common Divisor of two numbers is the largest positive numbers which can divide both numbers without any remainder

Program/Source Code

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 : ");
Console.Write("Enter the Second Number : ");
Console.Write("\nThe Greatest Common Divisor of ");
Console.WriteLine("{0} and {1} is {2}", x, y, GCD(x, y));
return 0;
}
}```
Program Explanation

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.

Runtime Test Cases
```
Enter the First Number : 12
Enter the Second Number : 24
The Greatest Common Divisor of 12 and 24 is : 12```

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

Note: Join free Sanfoundry classes at Telegram or Youtube
If you wish to look at all C# Programming examples, go to 1000 C# Programs.

If you find any mistake above, kindly email to [email protected]