- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find GCD of two numbers
In mathematics, Greatest Common Divisor (GCD) is the largest possible integer, that divides both of the integers. The condition is that the numbers must be non-zero.
We will follow the Euclidean Algorithm to find the GCD of two numbers.
Input and Output
Input: Two numbers 51 and 34 Output: The GCD is: 17
Algorithm
findGCD(a, b)
Input: Two numbers a and b.
Output: GCD of a and b.
Begin if a = 0 OR b = 0, then return 0 if a = b, then return b if a > b, then return findGCD(a-b, b) else return findGCD(a, b-a) End
Example
#include<iostream> using namespace std; int findGCD(int a, int b) { //assume a is greater than b if(a == 0 || b == 0) return 0; //as a and b are 0, the greatest divisior is also 0 if(a==b) return b; //when both numbers are same if(a>b) return findGCD(a-b, b); else return findGCD(a, b-a); } int main() { int a, b; cout << "Enter Two numbers to find GCD: "; cin >> a >> b; cout << "The GCD is: " << findGCD(a,b); }
Output
Enter Two numbers to find GCD: 51 34 The GCD is: 17
- Related Articles
- Swift Program to Find GCD of two Numbers
- Java Program to Find GCD of two Numbers
- Kotlin Program to Find GCD of two Numbers
- Haskell program to find the gcd of two numbers
- Java program to find the GCD or HCF of two numbers
- Program to find GCD or HCF of two numbers in C++
- C++ Program to Find GCD of Two Numbers Using Recursive Euclid Algorithm
- GCD and LCM of two numbers in Java
- Find two numbers whose sum and GCD are given in C++
- Find out the GCD of two numbers using while loop in C language
- Program to compute gcd of two numbers recursively in Python
- Program to find GCD or HCF of two numbers using Middle School Procedure in C++
- GCD of more than two (or array) numbers in Python Program
- Python Program for GCD of more than two (or array) numbers
- C++ Program for GCD of more than two (or array) numbers?

Advertisements