C Program to find GCD of two numbers using recursion

In this tutorial, we will learn the writing program in C to find the GCD of two numbers using recursion. In the previous tutorial, we have learnt to write the c program to calculate GCD without recursion.

Program in C to find GCD using recursion

#include <stdio.h>
int gcd(int num1, int num2)
{
    if (num2 == 0)
        return num1;
    return gcd(num2, num1 % num2);
}

int main()
{
    int num1, num2, maxNum;
    printf("Program to find GCD or HCF of two numbers\n");
    printf("Enter the first number: ");
    scanf("%d", &num1);
    printf("Enter the second number: ");
    scanf("%d", &num2);
    printf("GCD or HCF of numbers %d and %d is %d ", num1, num2, gcd(num1, num2));
    return 0;
}

Output

C Program to find GCD of two numbers using recursion

Share on

Also Prepare Below Important Question

Interview Questions Categories

C Programming Interview Preparation

Core Java Programming Interview Preparation

Python Programming Interview Preparation