C Program to find LCM and HCF (GCD) of two numbers

on 04 September 2013

This is a program code to compute LCM (Least Common Multiple) and HCF (Highest Common Factor) of two numbers entered by user in C programming language. Note that LCM is also called lowest common multiple or smallest common multiple and HCF is also called greatest common divisor (GCD) or greatest common factor (GCF).

#include <stdio.h>
#include <conio.h>
 
int main() 
{
    int a,b,x,y,temp,gcd,lcm;
    printf("Enter first number:");
    scanf("%d",&x);
    printf("Enter second number:");
    scanf("%d",&y);
 
    a = x;
    b = y;
 
    while (b != 0) 
    {
        temp = b;
        b = a % b;
        a = temp;
    }
 
    gcd = a;
    lcm = (x*y)/gcd;
 
    printf("Greatest common divisor is %d\n", gcd);
    printf("Least common multiple is %d\n",lcm);
 
    getch();
    return 0;
}

Output


LCM and GCD in C Language Program

Note: As shown in the image above, you can compile the program using FireCMD shell.

0 comments:

Post a Comment