C++ Program to Calculate the GCD of Two Numbers using While Loop

Write C++ Program to Calculate the GCD of Two Numbers using While Loop

// CPP Program to Calculate the GCD of Two Numbers using While Loop

#include <iostream>

using namespace std;

int main()
{
	int n1, n2, m, n;

	cout << "Enter number 1 :--> ";
	cin >> n1;

	cout << "Enter number 2 :--> ";
	cin >> n2;

	m = n1;
	n = n2;

	while(n1 != n2)
	{
		if(n1 > n2)
		{
			n1 = n1 - n2;
		}
		else
		{
			n2 = n2 - n1;
		}

	}
	cout << "GCD of " << m << " and " << n << " is " << n1;
	return 0;
}

Output:

Enter number 1 :--> 220
Enter number 2 :--> 430

GCD of 220 and 430 is 10

≪ Previous | Next ≫