C Program to find GCD and LCM of 2 integers

This C Program to find GCD and LCM of 2 integers Explains Clearly Step by Step how to find HCF

GCD :
The greatest common divisor of two or more integers is the largest positive integer that is a
a divisor of both numbers.
For example, the GCD of 8 and 12 is 4.

LCM :
The least common multiple of two integers a and b, usually denoted by LCM(a, b).
It is the smallest positive integer that is divisible by both a and b.


#include <stdio.h>
void main()
{
int n1, n2, gcd, lcm, r, n, d;
printf(“Enter two numbers :\n”);
scanf(“%d %d”, &n1, &n2);
if (n1 > n2)
{
n = n1;
d = n2;
}
else
{
n = n2;
d = n1;
}
r = n % d ;
while (r != 0)
{
n = d;
d = r;
r = n % d;
}
gcd = d;
lcm = n1 * n2 / gcd;
printf(“GCD of %d and %d = %d\n”, n1, n2, gcd);
printf(“LCM of %d and %d = %d\n”, n1, n2, lcm);
}

Share this