Pages

Friday, November 13, 2015

C Program for lcm and gcd [simplified]

// Program for The greatest common divisor (GCD) and The least common multiple (LCM)

#include<stdio.h>
void main()
{
int a,b,i,gcd;
printf("Enter the 2 numbers:");
scanf("%d %d",&a,&b);
if(a>b)
{

for(i=1;i<a;i++)
{
if(a%i==0 && b%i==0)
gcd=i;
}
}
if(a<b)
{

for(i=1;i<b;i++)
{
if(a%i==0 && b%i==0)
gcd=i;
}
}
printf("gcd = %d and lcm = %d",gcd,(a*b)/gcd);


}

Output
Enter the 2 numbers:
90
50
gcd = 10 and lcm=450

No comments:

Post a Comment