C++ Program to Find GCD of Two Numbers Using Recursive Euclid Algorithm

This is a C++ Program to find GCD of two numbers using Recursive Euclid Algorithm. In mathematics, the Euclidean algorithm, or Euclid’s algorithm, is a method for computing the greatest common divisor (GCD) of two (usually positive) integers, also known as the greatest common factor (GCF) or highest common factor (HCF). It is named after the Greek mathematician Euclid, who described it in Books VII and X of his Elements.

The GCD of two positive integers is the largest integer that divides both of them without leaving a remainder (the GCD of two integers in general is defined in a more subtle way).

In its simplest form, Euclid’s algorithm starts with a pair of positive integers, and forms a new pair that consists of the smaller number and the difference between the larger and smaller numbers. The process repeats until the numbers in the pair are equal. That number then is the greatest common divisor of the original pair of integers.

The main principle is that the GCD does not change if the smaller number is subtracted from the larger number. For example, the GCD of 252 and 105 is exactly the GCD of 147 (= 252 – 105) and 105. Since the larger of the two numbers is reduced, repeating this process gives successively smaller numbers, so this repetition will necessarily stop sooner or later — when the numbers are equal (if the process is attempted once more, one of the numbers will become 0).

Here is source code of the C++ Program to Find GCD of Two Numbers Using Recursive Euclid Algorithm. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. #include<iostream>
  2. #include<conio.h>
  3. #include<stdlib.h>
  4.  
  5. using namespace std;
  6. int gcd(int u, int v)
  7. {
  8.     return (v != 0) ? gcd(v, u % v) : u;
  9. }
  10.  
  11. int main(void)
  12. {
  13.     int num1, num2, result;
  14.     cout << "Enter two numbers to find GCD using Euclidean algorithm: ";
  15.     cin >> num1 >> num2;
  16.     result = gcd(num1, num2);
  17.     if (gcd)
  18.         cout << "\nThe GCD of " << num1 << " and " << num2 << " is: " << result
  19.                 << endl;
  20.     else
  21.         cout << "\nInvalid input!!!\n";
  22.     return 0;
  23. }

Output:

advertisement
advertisement
$ g++ GCDEuclidean.cpp
$ a.out
 
Enter two numbers to find GCD using Euclidean algorithm: 12 30
The GCD of 12 and 30 is: 6

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

Note: Join free Sanfoundry classes at Telegram or Youtube

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

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.