-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgcd.cpp
More file actions
26 lines (23 loc) · 860 Bytes
/
Copy pathgcd.cpp
File metadata and controls
26 lines (23 loc) · 860 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/* TODO: write a program that reads two integers and prints their greatest
* common divisor. E.g., if the two integers were 12 and 18, then your
* program should print 6. If the numbers were 12 and 19, it should print 1.
* NOTE: there is a nice way to do this, known as the Euclidean Algorithm,12
* but my intention is for you to just "brute force" search for the gcd. */
#include <iostream>
using std::cin;
using std::cout;
int main()
{
int a, b;
cin >> a >> b; //reads the 2 intg input
int gcd=1; // starts with 1, divisor of all integers
for(int i=1; i<=(a < b ? a : b); i++){ //This loop goes through all numbers from 1 to the smaller of a or b.
if (a % i == 0 && b % i == 0){
gcd = i; // Update gcd if i divides both a and b
}
}
cout << gcd << std::endl;
/* your answer goes here... */
return 0;
}
// vim:foldlevel=2