Wednesday, 22 October 2014

GCD and LCM of two numbers ( C Program )

Non-Recursive C Program:

Aim: To find GCD and LCM of Given two numbers.

Program:
#include <stdio.h>
int main()
{
  int n1,n2,num,den,rem, gcd, lcm;
printf("Enter two integers:\n"); scanf("%d%d", &n1, &n2);
  if(n1>n2)
  {
    num=n1;
    den=n2;
   }
  else
  {
    num=n1;
    den=n2;
   }
rem=num/den;
    while (rem!= 0)
    { 
       num = den;
       den = rem;
       rem = num/den;
     }
   gcd = den;
   lcm = (n1*n2)/gcd;
printf("\nGreatest common divisor of %d and %d is %d\n", n1, n2, gcd);
printf("Least common multiple of %d and %d is %d\n", n1, n2, lcm);
return 0;
}

Output:
Enter two integers:
24
9
Greatest common divisor of 24 and 9 is 3
Least common multiple of 24 and 9 is 72

Click Here to view programs list

No comments:

Post a Comment