GCD of two numbers in C

05 min read

C program to find the GCD of two numbers is given below. The  GCD of two integers is the largest integer that can exactly divide both numbers without any remainder. 

#include <stdio.h>
int main()
{
int n1, n2, i, gcd;
printf("Enter two integers: ");
scanf("%d %d", &n1, &n2);
for(i=1; i <= n1 && i <= n2; ++i)      //i  loops from 1, until it is greater than both n1 and n2
{
if(n1%i==0 && n2%i==0)                //Every i value which is less than n1 and n2 , should divide n1 and n2 perfectly.
gcd = i;
 }
printf("G.C.D of %d and %d is %d", n1, n2, gcd);
return 0;
}

 

Output:

Enter two integers: 81 153 GCD = 9
POST A NEW COMMENT
     
  • Input (stdin)

    Output (stdout)


    Input (stdin)

    Your Output (stdout)

    Expected Output

    Compiler Message

    Input (stdin)

    2    3

    Your Output (stdout)

    5

    Expected Output

    5

    Compiler Message

    5

    Error