Wednesday, 22 October 2014

GCD and LCM of two numbers ( C Program )

Recursive C Program:

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

Program:
#include <stdio.h>
long gcd(long, long);
void main()
{
  long n1, n2, hcf, lcm;
printf("Enter two integers:\n"); scanf("%ld%ld", &x, &y);
   hcf = gcd(n1, n2);
   lcm = (n1*n2)/hcf;
printf("Greatest common divisor of %ld and %ld = %ld\n", x, y, hcf); printf("Least common multiple of %ld and %ld = %ld\n", x, y, lcm);
return 0;
}
long gcd(long a, long b)
  {
     if (b == 0)
        return (a);
     else
        return (gcd(b, a % b));
  }

Output:
Enter two integers:
24
9
Greatest common divisor 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