C

GCD Program in C

In this tutorial, we are going to see how to write a GCD program in C. 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
#include <stdio.h>

int main()
{
    int nbr1, nbr2, gcd, i;
  
    printf("Enter two numbers: ");
    scanf("%d %d", &nbr1, &nbr2);
  
    for(i=1; i <= nbr1 && i <= nbr2; ++i)
    {
        if(nbr1%i==0 && nbr2%i==0)
            gcd = i;
    }
  
    printf("GCD of %d and %d = %d", nbr1, nbr2, gcd);
    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 *