Pages

Tuesday, November 10, 2015

C Program for lcm and gcd

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

#include<stdio.h>
#include<conio.h>
void main()
{
 int a,b,gcd,p,q;
 long lcm;
 clrscr();
 printf("Enter the 2 numbers:\n");
 scanf("%d\n%d",&a,&b);
 p=a;
 q=b;
 if(p==0)
 {
  gcd=q;
 }
 else if(q==0)
 {
  gcd=p;
 }
 else
 {
  while(q!=0)
  {
   if(p>q)
   {
    p=p-q;
   }
   else
   {
    q=q-p;
   }
  gcd=p;
  }
  }
 lcm=(a*b)/gcd;
 printf("LCM of %d and %d is %ld\n",a,b,lcm);
 printf("GCD of %d and %d is %d\n",a,b,gcd);
 getch();
}

Output
Enter the 2 numbers:
90
50
LCM of 90 and 50 is 450
GCD of 90 and 50 is 10

No comments:

Post a Comment