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

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

**Next Steps:**

- Get Free Certificate of Merit in C# Programming
- Participate in C# Programming Certification Contest
- Become a Top Ranker in C# Programming
- Take C# Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

**Related Posts:**

- Apply for Computer Science Internship
- Buy Computer Science Books
- Apply for C# Internship
- Buy MCA Books
- Practice Computer Science MCQs