C program to find GCD of two Numbers

C program to find GCD of two Numbers

1285
19
SHARE

C program to find of two numbers : GCD stands for Greatest common divisor. The greatest common divisor is the largest integer that divides both number1 and number2 without a remainder. GCD is also known as  the greatest common factor (gcf), or highest common factor (hcf).

Lets understand the concept of GCD using an example :

Suppose we want to calculate the GCD of 54 and 24.

Now we need to find the factors(all the that divide 54) of 54. The factors of 54 are :

1,2,3,6,9,18,27,54

Similarly, factor of 24 are :

1,2,3,4,6,8,12,24

Now the common factors of 54 are 1,2,3,6 out of which 6 is the greatest.

So 6 is the GCD i.e. Greatest common divisor of 54 and 24.

Now let us see how to write a C program to find GCD of two numbers using Recursive Euclid’s algorithm.

C program to find GCD of two numbers :

#include<stdio.h>
#include<conio.h>
int gcd(int num1, int num2) {
  if (num2)
    return gcd(num2, num1 % num2);
  else 
    return num1 < 0 ? -num1 : num1;
}
int main()
{
  int a,b,c;
  printf("\nEnter two numbers : ");
  scanf("%d%d",&a,&b);
  c= gcd(a,b);
  printf("\nGCD of %d and %d is : %d",a,b,c);
  getch();
  return 0;
}

We hope you all have enjoyed the C program to find GCD of two numbers. If you have any issues with the logic or the code ask me in form of comments.

19 COMMENTS

    • A: You are not alone. Remember this is new to a lot of you out there. In the past companies suibmtted blindly to the Internet in hopes of getting picked up. Well now you have a problem cleaning all the old stuff up. It can be done. Yes we can do it. We have to establish your preferred local area, get a work phone number and make the appropriate changes to your web site. Then it is just followup and a bit of a waiting game with all the existing places.

  1. Terrific work! This is the type of information that should be shared around the web. Shame on the search engines for not positioning this post higher!

  2. I really like your blog.. very nice colors & theme. Did you create this website yourself or did you hire someone to do it for you? Plz reply as I’m looking to design my own blog and would like to know where u got this from. many thanks

  3. With havin so much written content do you ever run into any problems of plagorism or copyright infringement? My blog has a lot of exclusive content I’ve either authored myself or outsourced but it appears a lot of it is popping it up all over the web without my authorization. Do you know any methods to help reduce content from being stolen? I’d genuinely appreciate it.

    • Yes ! you can use DMCA website for protecting your content or you can give it a try to tynt which posts your website link as reference if someone copies data from your website.

  4. The solution to achieve a state of not any thought is often a tire-iron to the back with the head. I can¡¯t understand why anyone may wish to achieve a state of no thought, but should you wanna undertake it than topple yourself available pun supposed.

  5. your gcd program is good. i’m just starting to study c program. can you please help me with this? can you help me build gcd using functions in c program? cheers!

  6. If we know the LCM of two numbers (let’s say A and B) then we can find GCD of A and B using following expression:
    LCM(A, B) X GCDA, B) = A*B

LEAVE A REPLY