C Program on GCD and LCM

Ram Pothuraju


This C program finds the Greatest Common Divisor (GCD) and Least Common Multiple (LCM)of 2 given numbers. 


#include<stdio.h>
#include<conio.h>
#include<math.h>

void main()
{
int a,b,c,n1,n2,gcd,lcd;
clrscr();
printf("\nEnter number 1:");
scanf("%d",&n1);
printf("\nEnter number 2:");
scanf("%d",&n2);
if(n1>n2)
{
a=n2;
b=n1;
}
else
{
a=n1;
b=n2;
}
while(a>0)
{
c=b%a;
if(c==0)
{
gcd=a;
break;
}
b=a;
a=c;
}
lcd=(n1*n2)/gcd;
printf("\nLCM is: %d",lcd);
printf("\nGCD is: %d",gcd);
getch();
}

Post a Comment

0Comments

Post a Comment (0)