Find Greatest Common Divisor (GCD) of two numbers c++ program


Levels of difficulty: / perform operation:

What is GCD of two numbers?
It means a greatest number which divides both numbers
For example: Two numbers are 18 and 24
Numbers which divides both are 1, 2, 3 and 6 in which greatest number is 6
So 6 is the GCD of 18 and 24



C++ Program

#include<iostream>
using namespace std;
int main() {
	int first_number;
	cout<<"Enter First Number : ";
	cin>>first_number;
	int  second_number;
	cout<<"Enter Second Number: ";
	cin>>second_number;
	int  gcd;
	for (int i=1;i<=first_number&&i<=second_number;i++) {
		if(first_number%i==0 && second_number%i == 0 ) {
			gcd=i;
		}
	}
	cout<<"Greatest Common Divison (GCD):"<<gcd<<endl;
	return 0;
}





Quantitative Aptitude
Reasoning
Programming
Interview