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 : ");
        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));
        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.

Note: Join free Sanfoundry classes at Telegram or Youtube

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.

Take C# Programming Practice Tests - Chapterwise!
Start the Test Now: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
If you wish to look at all C# Programming examples, go to 1000 C# Programs.

Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.