CALCULATION OF GCD & LCM OF TWO NUMBERS


#include<stdio.h>
#include<conio.h>
void main()
{
int lcm = 1, gcd = 1, a, b, i;
printf("Enter two numbers to calculate LCM & GCD");
scanf("%d%d",&a,&b);
for(i = 1; i < a; i++)
{
if(a % i == 0 && b % i == 0)
{
if(lcm < i)
{
lcm = i;
break;
}
}
}
for(i = 1; i < a; i++)
{
if(i * lcm == a)
gcd *= i;
if(i * lcm == b)
gcd *= i;
}
gcd *= lcm;
printf("LCM = %d GCD = %d",lcm,gcd);
}

0 comments: (+add yours?)

Post a Comment