Print the
greatest common divisor (GCD) between two numbers in C#
"The greatest common divisor of two integers is the largest integer that evenly divides each of the two numbers. Write method Gcd that returns the greatest common divisor of two integers.
C# Code
public static void
GCDNumber() { // GCD
Number are known as Greatest Common Divisor
//
number which is the largest number that divides both number //
Example // 3 =
3 // 6 =
2 * 3 // So
the GCD of 3 and 6 is: 3 int num1,
num2;
Console.Write("Enter the First number: "); num1
= Convert.ToInt32(Console.ReadLine());
Console.Write("Enter the second number: "); num2
= Convert.ToInt32(Console.ReadLine()); while (num1
!= num2) // numb1 not equal to num2
{ if (num1
> num2) // check num1 is greater than num2 {
num1 = num1 - num2; // subtract num2 form num1 } else {
num2 = num2 - num1; // else subtract num1 from
num2 } } if (num1
== 1) {
Console.Write("No GCD Number"); } else { Console.Write("GCD
Number: " + num1); } } |
Output
EmoticonEmoticon