C Code 4 _ To find the Greatest Common Divisor (GCD) of two given numbers
Program :
#include<stdio.h>
int main()
{
int n1,n2,s,i;
int gcd;
printf("Enter two numbers : ");
scanf("%d %d",&n1,&n2);
if(n1==0||n2==0)
{
gcd=0;
}
if(n1==n2)
{
gcd=n1;
}
if(n1<n2)
{
s=n1;
}
else
{
s=n2;
}
for(i=s;i>=1;i--)
{
if(n1%i==0&&n2%i==0)
{
gcd=i;
break;
}
}
printf("The gcd of %d and %d is %d",n1,n2,gcd);
getch();
return 0;
}
Output:
Thank You ...
0 comments: