C program to find GCD of two Numbers

Mr Coder September 19, 2012 18

C program to find GCD 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 numbers 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.

18 Comments »

  1. brahmani September 22, 2012 at 3:54 pm - Reply

    Its really very good site,which providing c codes which are very usefull to us

    • Zul October 19, 2012 at 2:20 am - Reply

      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.

  2. Beats By Dre Pas Cher September 25, 2012 at 4:10 am - Reply

    That’s a nice post.

  3. Louis Vuitton September 25, 2012 at 7:00 pm - Reply

    It’s a good post.

  4. Teach your dog September 28, 2012 at 6:17 am - Reply

    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!

    • Lynn October 20, 2012 at 12:34 am - Reply

      Most help articles on the web are inaccrutae or incoherent. Not this!

  5. regim hotelier timisoara October 10, 2012 at 7:25 pm - Reply

    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

    • Mr Coder October 10, 2012 at 7:27 pm - Reply

      I made it myself fren…

  6. buy wow gold October 11, 2012 at 1:12 am - Reply

    Itˇs really a nice and helpful piece of information. Iˇm glad that you just shared this helpful info with us. Please stay us up to date like this. Thank you for sharing.

  7. Andrea A Johnson October 13, 2012 at 9:01 pm - Reply

    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.

    • Mr Coder October 19, 2012 at 5:33 pm - Reply

      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.

  8. Irina Vasilieva October 25, 2012 at 8:51 am - Reply

    Lots of beneficial in a row. I give rise to bookmarked your place.

  9. regim hotelier timisoara November 3, 2012 at 5:45 pm - Reply

    Pleasant page, I genuinely benefited from glossing over it, keep doing the hard writing.

  10. Flower November 4, 2012 at 12:30 am - Reply

    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.

  11. regim hotelier arad November 5, 2012 at 9:04 am - Reply

    This is really interesting, You are a very skilled blogger. I have joined your feed and look forward to seeking more of your wonderful post. Also, I’ve shared your web site in my social networks!

  12. rekha December 28, 2012 at 2:57 pm - Reply

    plz explain the logic i canot understand it.

  13. shahista khan January 12, 2013 at 1:54 pm - Reply

    thanks.It’s a very helpful post

  14. donald April 10, 2013 at 12:53 am - Reply

    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!

Leave A Response »