
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ Program to Find G.C.D Using Recursion
The Greatest Common Divisor (GCD) of two numbers is the largest number that divides both of them.
For example: Let’s say we have following two numbers: 45 and 27
63 = 7 * 3 * 3 42 = 7 * 3 * 2 So, the GCD of 63 and 42 is 21
A program to find the GCD of two numbers using recursion is given as follows.
Example
#include<iostream> using namespace std; int gcd(int a, int b) { if (a == 0 || b == 0) return 0; else if (a == b) return a; else if (a > b) return gcd(a-b, b); else return gcd(a, b-a); } int main() { int a = 63, b = 42; cout<<"GCD of "<< a <<" and "<< b <<" is "<< gcd(a, b); return 0; }
Output
GCD of 63 and 42 is 21
In the above program, gcd() is a recursive function. It has two parameters i.e. a and b. If a or b is 0, the function returns 0. If a or b are equal, the function returns a. If a is greater than b, the function recursively calls itself with the values a-b and b. If b is greater than a, the function recursively calls itself with the values a and b-a.
This is demonstrated by the following code snippet.
int gcd(int a, int b) { if (a == 0 || b == 0) return 0; else if (a == b) return a; else if (a > b) return gcd(a-b, b); else return gcd(a, b-a); }
Another method of finding the GCD of two numbers using recursion is as follows.
Example
#include <iostream> using namespace std; int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } int main() { int a = 63, b = 42; cout<<"GCD of "<< a <<" and "<< b <<" is "<< gcd(a, b); return 0; }
Output
GCD of 63 and 42 is 21
In the above program, gcd() is a recursive function. It has two parameters i.e. a and b. If b is greater than 0, then a is returned to the main() function. Otherwise, the gcd() function recursively calls itself with the values b and a%b.
This is demonstrated using the following code snippet.
int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); }
- Related Articles
- C++ Program to Find Fibonacci Numbers using Recursion
- C++ program to Find Sum of Natural Numbers using Recursion
- C++ Program to Find Factorial of a Number using Recursion
- Java Program to Find G.C.D Using Recursion
- Golang Program to Find G.C.D Using Recursion
- Haskell Program to Find G.C.D Using Recursion
- C++ Program to Find the Product of Two Numbers Using Recursion
- C++ Program to Calculate Power Using Recursion
- C# program to perform Quick sort using Recursion
- C++ program to Reverse a Sentence Using Recursion
- Python Program to Find the Fibonacci Series Using Recursion
- C# program to find the sum of digits of a number using Recursion
- Binary to Gray code using recursion in C program
- Python Program to Find the Fibonacci Series without Using Recursion
- Java Program to Find Factorial of a Number Using Recursion
