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("Find GCD or HCF of two numbers using recursion\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 1
Find GCD or HCF of two numbers using recursion
Enter the first number: 23
Enter the second number: 55
GCD or HCF of numbers 23 and 55 is 1
Output 2
Find GCD or HCF of two numbers using recursion
Enter the first number: 12
Enter the second number: 20
GCD or HCF of numbers 12 and 20 is 4
Also Prepare Below Important Question
- Hibernate Interview Questions for 2+ years of experience
- 68 Most Important Microservices Interview Questions
- 60 Most Important Git Interview Questions
- 50+ Mostly asked Java Interview Questions for 6 Years Exp.
- 60+ Mostly Asked Java Interview Questions for 4 Years Exp
- 60+ Mostly asked Java Interview Question for 3+ Years
- 60+ Spring Boot interview questions for 4+ years Exp.
- 60+ Mostly Asked Spring Boot Interview Questions for 3+ Yrs
- Scenario Based Java 8 Coding Interview Questions (For Experienced)
- Python Program to add two numbers without addition operator
- Mostly Asked Java Interview Questions For 2 Yrs Experience
- Find All Pairs in Array whose Sum is Equal to given number Using Java
- Java Program to find GCD of two Numbers using Recursion
- Python Program to Separate Characters in a Given String
- Python Program to add two number using Recursion
- Python Program to Find Highest Frequency Element in Array
- Python Program to Merge two Arrays
- Perform left rotation by two positions in Array Using Python
- Python Program to Delete Element at Given Index in Array
- Python Program to Delete element at End of Array
Interview Questions Categories
C Programming Interview Preparation
Core Java Programming Interview Preparation
- Core Java Programming Coding Questions
- Core Java Pattern Programming Questions
- Core Java Programming Interview Questions