C Program to Find GCD of Two Numbers using Recursion

This is a C Program to find gcd of given numbers using recursion.

Problem Description

This C program, using recursion, finds the GCD of the two numbers entered by the user.

Problem Solution

The user enters two numbers by using a space in between them or by pressing enter after each input.

Program/Source Code

Here is the source code of the C program to display a linked list in reverse. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

/*
 * C Program to find GCD of given Numbers using Recursion
 */
#include <stdio.h>
 
int gcd(int, int);
 
int main()
{
    int a, b, result;
 
    printf("Enter the two numbers to find their GCD: ");
    scanf("%d%d", &a, &b);
    result = gcd(a, b);
    printf("The GCD of %d and %d is %d.\n", a, b, result);
}
 
int gcd(int a, int b)
{
    while (a != b)
    {
        if (a > b)
        {
            return gcd(a - b, b);
        }
        else
        {
            return gcd(a, b - a);
        }
    }
    return a;
}
Program Explanation

In this C Program, we are reading the two integer numbers using ‘a’ and ‘b’ variable. The gcd() function is used to find the GCD of two entered integers using recursion.

advertisement
advertisement

While loop is used to check that both the ‘a’ and ‘b’ variable values are not equal. If the condition is true then execute the loop. Otherwise, if the condition is false return the value of ‘a’ variable. If else condition statement is used to check the value of ‘a’ variable is greater than the value of ‘b’ variable.

If the condition is true, then return two integer variable values. Otherwise, if the condition is false, then execute else statement and return the values of two integer variable. Print the GCD of a given number using printf statement.

Runtime Test Cases
 
$ gcc gcd_recr.c -o gcd_recr
$ a.out
Enter the two numbers to find their GCD: 100 70
The GCD of 100 and 70 is 10.

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at other example programs on Mathematical Functions, go to C Programming Examples on Mathematical Functions. If you wish to look at programming examples on all topics, go to C Programming Examples.

advertisement
If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.