Search This Blog

Tuesday, 9 June 2015

A program to calculate GCD for two given numbers

class Class3
{
static void Main(string[]args)
{
int a,b,c,i,j;
Console.WriteLine("Enter the first number");
a=int.Parse(Console.ReadLine());
Console.WriteLine("Enter the second number");
b=int.Parse(Console.ReadLine());
if (a>b)
i=b;
else
i=a;
for(j=i;j>1;j--)
{
if (a%j==0&&b%j==0)
break;
}
Console.WriteLine("The greatest common divisor of {0} and {1} is {2}",a,b,j);
Console.ReadLine();
}
}

OUTPUT

CLICK TO DOWNLOAD


No comments:

Post a Comment