C++ program to find GCD of Two Numbers using Recursion
Write C++ program to find GCD of Two Numbers using Recursion
// CPP program to find GCD of Two Numbers using Recursion #include <iostream> using namespace std; int gcd(int n1, int n2) { while(n1 != n2) { if(n1 > n2) { n1 = gcd(n1 - n2, n2); } else { n2 = gcd(n1, n2 - n1); } } return n1; } int main() { int n1,n2; cout << "Enter Number 1 :--> "; cin >> n1; cout << "Enter Number 2 :--> "; cin >> n2; int result = gcd(n1, n2); cout << "GCD of " << n1 << " and " << n2 << " is " << result; return 0; }
Output:
Enter Number 1 :--> 56
Enter Number 2 :--> 42
GCD of 56 and 42 is 14