C++ Program to Find GCD of Two Numbers using While loop | Function | Recursion
C++ program to find gcd of two numbers using while loop, function, and recursion.
In this article, you will learn how to make a C++ program to find gcd of two numbers using while loop, function, and recursion.
Example
-----Enter the two positive integer numbers-----
180
50
The GCD number of 180 & 50 is: 10
You should have the knowledge of the following topics in c++ programming to understand these programs:
- C++
main()
function - C++
cin
object - C++
cout
object - C++ For loop
- C++ While loop
- C++ Functions
- C++ Recursion
What is GCD Number?
In this article, we solve this problem in four methods:
- Using the for loop
- Using the while loop
- Using the functions
- Using the recursion
Source Code
// C++ Program to Find GCD of Two Numbers using For loop
#include <iostream>
using namespace std;
int main() {
int p, q, i, g;
// p & q - To store the two positive numbers
cout << "-----Enter the two positive integer numbers-----\n";
cin >> p >> q;
for (i = 1; i <= p && i <= q; ++i) {
if (p % i == 0 && q % i == 0) {
g = i;
}
}
cout << "\nThe GCD number of " << p << " & " << q << " is: " << g << "\n";
return 0;
}
Output
-----Enter the two positive integer numbers-----
180
50
The GCD number of 180 & 50 is: 10
Explanation
In this given program, we have taken two inputs 180
and 50
from the user via the system console. Then we applied calculation to solve this problem.
After the above calculation, this will return 10
the final output of the above program.
C++ Program to Find GCD of Two Numbers using While loop
// C++ Program to Find GCD of Two Numbers using While loop
#include <iostream>
using namespace std;
int main() {
int p, q;
// p & q - To store the two positive numbers
cout << "-----Enter the two positive integer numbers-----\n";
cin >> p >> q;
while (p != q) {
if (p > q) {
p -= q;
} else {
q -= p;
}
}
cout << "\nThe GCD number is: " << p << "\n";
return 0;
}
C++ Program to Find GCD of Two Numbers using Function
// C++ Program to Find GCD of Two Numbers using Function
#include <iostream>
using namespace std;
// This function will find the GCD number
void FindGCD(int x, int y) {
int gcd, i;
for (i = 1; i <= x && i <= y; ++i) {
if (x % i == 0 && y % i == 0) {
gcd = i;
}
}
cout << "\nThe GCD number of " << x << " & " << y << " is: " << gcd << "\n";
}
// It's the driver function
int main() {
int p, q;
// p & q - To store the two positive numbers
cout << "-----Enter the two positive integer numbers-----\n";
cin >> p >> q;
FindGCD(p, q);
return 0;
}
Output
-----Enter the two positive integer numbers-----
45
90
The GCD number of 45 & 90 is: 45
C++ Program to Find GCD of Two Numbers using Recursion
// C++ Program to Find GCD of Two Numbers using Recursion
#include <iostream>
using namespace std;
// This function will do recursive
int FindGCD(int x, int y) {
if (y != 0)
return FindGCD(y, x % y);
else
return x;
}
// It's the driver function
int main() {
int p, q;
// p & q - To store the two positive numbers
cout << "-----Enter the two positive integer numbers-----\n";
cin >> p >> q;
cout << "\nThe GCD number of " << p << " & " << q << " is: " << FindGCD(p, q);
cout << endl;
return 0;
}
Also, visit these links
C Program to Find Factors of a Number