C Program to Find GCD of given Numbers using Recursion

This C program, using recursion, finds the GCD of the two numbers entered by the user. The user enters two numbers by using a space in between them or by pressing enter after each input.

Here is the source code of the C program to display a linked list in reverse. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to find GCD of given Numbers using Recursion
  3.  */
  4. #include <stdio.h>
  5.  
  6. int gcd(int, int);
  7.  
  8. int main()
  9. {
  10.     int a, b, result;
  11.  
  12.     printf("Enter the two numbers to find their GCD: ");
  13.     scanf("%d%d", &a, &b);
  14.     result = gcd(a, b);
  15.     printf("The GCD of %d and %d is %d.\n", a, b, result);
  16. }
  17.  
  18. int gcd(int a, int b)
  19. {
  20.     while (a != b)
  21.     {
  22.         if (a > b)
  23.         {
  24.             return gcd(a - b, b);
  25.         }
  26.         else
  27.         {
  28.             return gcd(a, b - a);
  29.         }
  30.     }
  31.     return a;
  32. }}


$ gcc gcd_recr.c -o gcd_recr
$ a.out
Enter the two numbers to find their GCD: 100 70
The GCD of 100 and 70 is 10.

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Reference Books in C Programming, Data-Structures and Algorithms

If you wish to look at other example programs on Mathematical Functions, go to C Programming Examples on Mathematical Functions. If you wish to look at programming examples on all topics, go to C Programming Examples.
Subscribe Newsletter & Posts
About

Manish Bhojasia, a technology veteran with 17+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer and SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage & Cluster Administration, Advanced C Programming, SAN Storage Technologies, SCSI Internals and Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him below.


Sanfoundry Training Center of Excellence (CoE)

Manish Bhojasia, a technology veteran with 17+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer and SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage & Cluster Administration, Advanced C Programming, SAN Storage Technologies, SCSI Internals and Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him below.







Subscribe Newsletter & Posts
Follow Manish & Sanfoundry