- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

The Greatest Common Divisor (GCD) of two numbers is the largest number that divides both of them.

For example: Let’s say we have two numbers are 45 and 27.

45 = 5 * 3 * 3 27 = 3 * 3 * 3

So, the GCD of 45 and 27 is 9.

A program to find the GCD of two numbers is given as follows.

#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 = 105, b = 30; cout<<"GCD of "<< a <<" and "<< b <<" is "<< gcd(a, b); return 0; }

GCD of 105 and 30 is 15

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 by the following code snippet −

int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); }

Another program to find the GCD of two numbers is as follows −

#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 = 105, b =30; cout<<"GCD of "<< a <<" and "<< b <<" is "<< gcd(a, b); return 0; }

GCD of 105 and 30 is 15

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); }

- Related Questions & Answers
- Write a C# program to find GCD and LCM?
- Program to find GCD of floating point numbers in C++
- C program to find GCD of numbers using recursive function
- C++ Program to Find the GCD and LCM of n Numbers
- Program to find GCD or HCF of two numbers in C++
- C program to find GCD of numbers using non-recursive function
- C++ Program to Find GCD of Two Numbers Using Recursive Euclid Algorithm
- Queries to update a given index and find gcd in range in C++ Program
- Java program to find the GCD or HCF of two numbers
- Find GCD of two numbers
- Program to find GCD or HCF of two numbers using Middle School Procedure in C++
- Find original numbers from gcd() every pair in C++
- How to Find HCF or GCD using Python?
- Find any pair with given GCD and LCM in C++
- Find pair with maximum GCD in an array in C++

Advertisements