Saturday, 30 May 2015

C program to find hcf and lcm using recursion.

#include <stdio.h>
 #include<conio.h>
long gcd(long, long);

int main() {
  long x, y, hcf, lcm;
  clrscr();
  printf("Enter two integers\n");
  scanf("%ld%ld", &x, &y);

  hcf = gcd(x, y);
  lcm = (x*y)/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);

  getch();
}

long gcd(long a, long b) {
  if (b == 0) {
    return a;
  }
  else {
    return gcd(b, a % b);
  }
}

Output:-
Enter two integers
9
24
Greatest common divisor of 9 and 24 = 3

Least common multiple of 9 and 24 = 72

No comments:

Post a Comment