x

C program to find hcf (gcd) and lcm

C program to find HCF (GCD) and LCM:

#include <stdio.h>


int main()

{

int a, b, x, y, temp, gcd, lcm;

printf(“Please enter two numbers one by one\n”);

scanf(“%d”, &x);

scanf(“%d”, &y);

 

a = x;

b = y;

 

while (b != 0)

{

temp = b;

b = a % b;

a = temp;

}

 

gcd = a;

lcm = (x*y)/gcd;


 

printf(“GCD:\nGreatest common divisor of the numbers %d and %d = %d\n”, x, y, gcd);

printf(“LCM:\nLeast common multiple of the numbers %d and %d = %d\n”, x, y, lcm);

 

return 0;

}

 

Output:

Please enter two numbers one by one

8

6

GCD:

Greatest common divisor of the numbers 8 and 6 = 2

LCM:

Least common multiple of the numbers 8 and 6 = 24



Like it? Please Spread the word!