C Program To Find GCD and LCM of Two Numbers using Euclidean algorithm

Lets write a C program to find GCD / HCF and LCM of Two user entered Numbers using Euclidean algorithm.

Full Forms

GCD: Greatest Common Divisor.
HCF: Highest Common Factor.
LCM: Least common Multiple.

Related Read:
while loop in C programming

Formula To Calculate LCM

Once we get the GCD, we use the below formula to calculate LCM.

LCM = ( num1 * num2 ) / GCD;

Source Code: C Program To Find GCD and LCM of Two Numbers using Euclidean algorithm

 
#include < stdio.h >

int main()
{
    int num1, num2, gcd, lcm, rem, numerator, denominator;

    printf("Enter 2 integer numbers\n");
    scanf("%d%d", &num1, &num2);

    if(num1 > num2)
    {
        numerator   = num1;
        denominator = num2;
    }
    else
    {
        numerator   = num2;
        denominator = num1;
    }

    rem = numerator % denominator;

    while(rem != 0)
    {
        numerator   = denominator;
        denominator = rem;
        rem         = numerator % denominator;
    }

    gcd = denominator;
    lcm = (num1 * num2) / gcd;

    printf("GCD of %d and %d is %d\n", num1, num2, gcd);
    printf("LCM of %d and %d is %d\n", num1, num2, lcm);

    return 0;
}

Output
Enter 2 integer numbers
15
20
GCD of 15 and 20 is 5
LCM of 15 and 20 is 60

C Program To Find GCD and LCM of Two Numbers using Euclidean algorithm


[youtube https://www.youtube.com/watch?v=liDKluluORo]

YouTube Link: https://www.youtube.com/watch?v=liDKluluORo [Watch the Video In Full Screen.]


Logic To Find GCD and LCM of Two Numbers using Euclidean algorithm

We find biggest of the 2 numbers entered by the user. Biggest number is assigned to variable numerator and smaller number is assigned to variable denominator. Now reminder is calculated. If the reminder is not equal to zero, then the value of variable denominator is assigned to variable numerator and the value of variable reminder is transferred to variable denominator and once again reminder is calculated – until reminder is zero. Once reminder is zero, gcd will be present in variable denominator.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Find GCD and LCM of Two Numbers

Lets write a C program to find GCD(Greatest Common Divisor) or HCF(Highest common Factor) and LCM(Least Common Multiple) of 2 user entered integer numbers.

Related Read:
while loop in C programming
Relational Operators In C
Logical Operators In C

GCD or HCF: Largest Integer that can divide both the numbers without any remainder or with 0 as remainder.
C Program to Find GCD or HCF of Two Numbers

Least Common Multiple(LCM): is the smallest positive integer that is perfectly divisible by the given integer values.
C Program to Find LCM of Two Numbers
Biggest of Two Numbers Using Ternary Operator: C

Logic To Find GCD and LCM of Two Integer Numbers.

We ask the user to enter 2 integer numbers. Next we find the smallest number among the two. Example, if num1 = 2 and num2 = 3. We can’t have any number bigger than 2, which will divide num1 and have reminder as 0.

Further explanation to find GCD or HCF is present in detail in this article: C Program to Find GCD or HCF of Two Numbers

Formula To Calculate LCM

Once we get the GCD, we use the below formula to calculate LCM.

LCM = ( num1 * num2 ) / GCD;

Source Code: C Program To Find GCD and LCM of Two Numbers

 
#include < stdio.h >

int main()
{
    int num1, num2, gcd, lcm, count = 1, small;

    printf("Enter 2 integer numbers\n");
    scanf("%d%d", &num1, &num2);

    small = (num1 < num2) ? num1 : num2;

    while(count <= small)
    {
        if(num1 % count == 0 && num2 % count == 0)
        {
            gcd = count;
        }
        count++;
    }

    lcm = ( num1 * num2 ) / gcd;

    printf("GCD = %d\nLCM = %d\n", gcd, lcm);

    return 0;
}

Output 1:
Enter 2 integer numbers
30
40
GCD = 10
LCM = 120

Output 2:
Enter 2 integer numbers
12
24
GCD = 12
LCM = 24

C Program To Find GCD and LCM of Two Numbers


[youtube https://www.youtube.com/watch?v=WftFkLOFhsA]

YouTube Link: https://www.youtube.com/watch?v=WftFkLOFhsA [Watch the Video In Full Screen.]


For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program to Find LCM of Two Numbers

Lets write a C program to find Least Common Multiple (LCM) of 2 integer numbers.

Related Read:
while loop in C programming
Biggest of Two Numbers Using Ternary Operator: C

Least Common Multiple(LCM): is the smallest positive integer that is perfectly divisible by the given integer values.

Logic to Find LCM of Two Numbers

We assign variable fact with a initial value of 1. While loop keeps executing until fact value is 0. We ask the user to enter 2 integer numbers. Now we find the biggest of these 2 numbers and store it inside variable lcm. We can even skip this step – in which case while loop has to execute more number of times. To optimize the code we find biggest of the 2 user input numbers and store it inside variable lcm – we do this because, we need to find a number which is perfectly divisible by both the user entered number, so obviously the number should be greater than or equal to the biggest number entered by the user.

Next, inside while loop we check if the value present in variable lcm is perfectly divisible by both num1 and num2. Once the condition is true, we display the result on to the console window and assign value 0 to variable fact. Once fact is 0, control exits while loop.

If the value of lcm is not perfectly divisible by both num1 and num2, then we increment the value of variable lcm by 1 and check for divisibility once again. We keep executing the while loop and keep incrementing the value of lcm by 1 until the value present in lcm is perfectly divisibile by value of num1 and num2.

Source Code: C Program to Find LCM of Two Numbers

 
#include < stdio.h >

int main()
{
    int num1, num2, lcm, fact =1;

    printf("Enter 2 numbers\n");
    scanf("%d%d", &num1, &num2);

    lcm = (num1 > num2) ? num1 : num2;

    while(fact)
    {
        if(lcm % num1 == 0 && lcm % num2 == 0)
        {
            printf("LCM of %d and %d is %d\n", num1, num2, lcm);
            fact = 0;
        }
        lcm++;
    }

    return 0;
}

Output 1:
Enter 2 numbers
24
60
LCM of 24 and 60 is 120

Output 2:
Enter 2 numbers
2
3
LCM of 2 and 3 is 6

C Program to Find LCM of Two Numbers


[youtube https://www.youtube.com/watch?v=XwSf-rvdkzo]

YouTube Link: https://www.youtube.com/watch?v=XwSf-rvdkzo [Watch the Video In Full Screen.]


Explanation With Example

If 2 numbers are num1 = 2 and num2 = 3.
We find biggest of these two numbers and store it inside variable lcm.
So 3 is greater than 2, so lcm = 3.

Iteration 1
fact = 1;
lcm = 3;

lcm % num1 = 3 % 2 = 1 // not equal to 0
lcm % num2 = 3 % 3 = 0 // equal to 0

Increment value of lcm by 1. So lcm = 4.

Iteration 2
fact = 1;
lcm = 4;

lcm % num1 = 4 % 2 = 0 // equal to 0
lcm % num2 = 4 % 3 = 1 // not equal to 0

Increment value of lcm by 1. So lcm = 5.

Iteration 3
fact = 1;
lcm = 5;

lcm % num1 = 5 % 2 = 2 // not equal to 0
lcm % num2 = 5 % 3 = 1 // not equal to 0

Increment value of lcm by 1. So lcm = 6.

Iteration 3
fact = 1;
lcm = 6;

lcm % num1 = 6 % 2 = 0 // equal to 0
lcm % num2 = 6 % 3 = 0 // equal to 0

Since both the condition are true
i.e., lcm%num1 == 0 && lcm%num2 == 0

We printout the value of variable lcm i.e., 6 as the Least Common Multiple of numbers 2 and 3. Next we assign 0 to variable fact. Since fact is 0, control exits while loop.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert0