Ticker

6/recent/ticker-posts

Header Ads Widget

Responsive Advertisement

LCM of Two Numbers using C

Problem Statement


In this program we will code a basic program to calculate LCM of two numbers in C Programming language.

The Least Common Multiple of two integers a and b, denoted by LCM, is the smallest positive integer that is divisible by both a and b.

An efficient solution is based on below formula for LCM of two numbers ‘a’ and ‘b’.

  LCM(a, b) = (a x b) / GCD(a, b)

Code

#include  
int gcd(int a, int b) 
{ 
    if (b == 0) 
        return a; 
    return gcd(b, a % b);  
} 
int main() 
{ 
    int n1,n2;
    printf("Enter two numbers : ");
    scanf("%d%d",&n1,&n2);
    int res= (n1 * n2) / gcd(n1, n2);
    printf("LCM of %d and %d is %d.", n1, n2, res); 
    return 0; 
} 

Output

Enter two numbers : 10 100

LCM of 10 and 100 is 100.

Post a Comment

0 Comments