• To check GCD of two numbers first we need to read input from user. i.e ask user to enter two numbers
  • Store those two numbers in two integer variables
  • Check if number is factor of given two numbers by iterating in for loop.

gcd of two numbers in c


  1. #include <stdio.h>
  2. // program to check gcd of two numbers in c 
  3. // write a c program to find gcd of two integers
  4. // www.instanceofjava.com
  5. int main()
  6. {
  7.     int number1, number2, i, gcd;
  8.     // read input from user
  9.     printf(" please enter any two numbers: ");
  10.     scanf("%d %d", &number1, &number2);

  11.     for(i=1; i <= number1 && i <= number2; ++i)
  12.     {
  13.         // checking  if i is divisible by both numbers / factor of both numbers
  14.         if(number1%i==0 && number2%i==0)
  15.             gcd = i;
  16.     }

  17.     printf("GCD of %d and %d is %d", number1, number2, gcd);

  18.     getch();
  19. }


Output:

gcd of two integers in c




Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

No comments

Leave a Reply

Select Menu