C

GCD Program in C Using Recursion

In this tutorial, we are going to see how to write a GCD program in C using recursion. The GCD or the Greatest Common Divisor of two numbers is the largest number that can divide exactly the two numbers (without remainder). Example:
 


 
 

GCD Program in C Using Recursion
#include <stdio.h>

int gcd(int nbr1, int nbr2)
{
    if (nbr2 != 0)
       return gcd(nbr2, nbr1%nbr2);
    else 
       return nbr1;
}

int main()
{
   int nbr1, nbr2;
   
   printf("Enter two numbers: ");
   scanf("%d %d", &nbr1, &nbr2);
   
   printf("GCD of %d and %d = %d", nbr1, nbr2, gcd(nbr1,nbr2));
   return 0;
}

Output:

Enter two numbers: 60 36
GCD of 60 and 36 = 12

 

mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *