In this program, we’ll learn to find Greatest Common Divisor (GCD) of two numbers in C#.
The HCF or GCD of two integers is the largest integer that can exactly divide both numbers (without a remainder).
 
First we write the pseudocode of the algorithm as follows. In the rest of the article you can find the C# code.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | BEGIN NUMBER n1 , n2 , gcd = 1, i OUTPUT "Enter first Number:" INPUT n1 OUTPUT "Enter second Number:" INPUT n2  FOR  i = 1; i <= n1 && i <= n2; ++i THEN 	IF n1 % i == 0 && n2 % i == 0 THEN 		gcd = i         END IF END FOR OUTPUT " G.C.D of "+n1+"and "+n1+" is "+ gcd END | 
C# Code:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |     class Program     {         static void Main(string[] args)         {             int n1 , n2 , gcd = 1;             Console.Write("Enter firstNumber:");             n1 = Convert.ToInt32(Console.ReadLine());             Console.Write("Enter firstNumber:");             n2 = Convert.ToInt32(Console.ReadLine());             for (int i = 1; i <= n1 && i <= n2; ++i)             {                 // Checks if i is factor of both integers                 if (n1 % i == 0 && n2 % i == 0)                     gcd = i;             }             Console.Write(" G.C.D of {0} and {1} is {2}", n1, n2, gcd);             Console.ReadKey();         }     } | 
Output:


 
							