I want to find the greatest common divisor between two input numbers, and I bumped into a problem.
I am not sure if the method I used to actually find the divisor is right. I did it by dividing both of the numbers with every number until the number reaches itself.
#include <iostream>
#include <string>
using namespace std;
int main() {
int num1, num2;
int large = 0;
int gcd = 0;
cout << "this program finds the greatest common divisor" << endl;
cout << "input first number > "; cin >> num1;
cout << "input second number > "; cin >> num2;
if (num1 > num2)
large = num1;
else
large = num2;
cout << "larger number > " << large << endl;
cout << endl;
for (int i = 0; i < large + 1; i++) {
if (num1 % i == 0 && num2 % i == 0) {
gcd = i;
}
}
cout << "The gcd of " << num1 << " and " << num2 << " is " << gcd << endl;
cout << endl;
}