C Program To Find GCD of Two Numbers using Recursion: Euclid’s Algorithm

Lets write a C program to find GCD(Greatest Common Divisor) or HCF(Highest Common Factor) of two positive integer numbers input by the user using Euclid’s Algorithm and by using Recursive function call logic.

Related Read:
C Program To Find GCD and LCM of Two Numbers using Euclidean algorithm
Recursive Functions In C Programming Language

Euclid’s Algorithm Logic

If user inputs 2 numbers n1 and n2, reduce the bigger number by modulo dividing it by the smaller number. For example, if n1 is greater than n2, then reduce the value of n1 by replacing it with n1%n2.

Assume that we’ve a function gcd() which returns gcd of 2 numbers passed to it. Ex: gcd(n1, n2);

According to Euclid’s Algorithm, we’ll get the same gcd if we reduce the bigger number by modulo dividing it by smaller number.

If n1 > n2 we need to pass gcd(n1%n2, n2);
If n2 > n1, we need to pass gcd(n1, n2%n1);

We need to recursively execute above 2 lines of logic until either n1 is 0 or until n2 is 0. If n1 is 0, then value present in n2 is the gcd of (n1,n2). If n2 is 0, then value present in n1 is the gcd of (n1,n2).

Video Tutorial: C Program To Find GCD of Two Numbers using Recursion: Euclid’s Algorithm


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

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


Source Code: C Program To Find GCD of Two Numbers using Recursion: Euclid’s Algorithm

#include<stdio.h>

int gcd(int, int);

int main()
{
    int num1, num2;

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

    printf("\nGCD of %d and %d is %d.\n", num1, num2, gcd(num1, num2));

    return 0;
}

int gcd(int n1, int n2)
{
    if(n1 == 0) return n2;
    if(n2 == 0) return n1;

    if(n1 > n2)
        return gcd(n1%n2, n2);
    else
        return gcd(n1, n2%n1);
}

Output 1:
Enter 2 positive integer numbers
1980
1617

GCD of 1980 and 1617 is 33.

Output 2:
Enter 2 positive integer numbers
15
20

GCD of 15 and 20 is 5.

Example:

Lets assume that user has entered n1 = 1980 and n2 = 1617

n1n2Biggest NoEvaluateFunction Call
198016171980gcd(1980 % 1617, 1617)gcd(363, 1617)
36316171617gcd(363, 1617 % 363)gcd(363, 165)
363165363gcd(363 % 165, 165)gcd(33, 165)
33165165gcd(33, 165 % 33)gcd(33, 0)

In above table gcd(33, 0) gets called, since n2 = 0, our program returns value of n1 as gcd, which is 33.

So GCD of 1980 and 1617 is 33.

Source Code: C Program To Find GCD of Two Numbers using Recursion and Ternary or Conditional Operator: Euclid’s Algorithm

#include<stdio.h>

int gcd(int, int);

int main()
{
    int num1, num2;

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

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

    return 0;
}

int gcd(int n1, int n2)
{
    if(n1 == 0) return n2;
    if(n2 == 0) return n1;

    return( (n1 > n2) ? gcd(n1%n2, n2) : gcd(n1, n2%n1) );
}

Output 1:
Enter 2 positive integer numbers
1980
1617

GCD of 1980 and 1617 is 33.

Know more about ternary operator or conditional operator, watch a separate video tutorial: Ternary Operator / Conditional Operator In C.

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 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 GCD or HCF of Two Numbers

Lets write a C program to find Greatest Common Divisor(GCD) or the Highest Common Factor(HCF) of 2 integer numbers entered by the user.

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.

Logic to Find GCD or HCF of Two Numbers

Variable count is initialized to 1. Next, we ask the user to enter 2 integer numbers. We check the smallest of the 2 numbers and we iterate the while loop until variable count is less than or equal to this smallest number.

Inside while loop we keep incrementing the value of count by one for every iteration. We also check if both numbers entered by the user is perfectly divisible by the value present in count. If we get any such number, we store it inside the variable gcd. And after the control exits while loop we print the value present in variable gcd.

Source Code: C Program to Find GCD or HCF of Two Numbers

 
#include < stdio.h >

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

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

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

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

    return 0;
}

OR

 
#include < stdio.h >

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

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

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

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

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

    return 0;
}

Output 1:
Enter 2 positive integers
24
60
GCD or HCF of 24 and 60 is 12

Here we check the smallest number among the two numbers entered by the user. We iterate through the while loop until value of count is less than or equal to that smallest number of the 2 numbers entered by the user.
Biggest of Two Numbers Using Ternary Operator: C

C Program to Find GCD or HCF of Two Numbers


[youtube https://www.youtube.com/watch?v=4ZbDj6BrM-Q]

YouTube Link: https://www.youtube.com/watch?v=4ZbDj6BrM-Q [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