Read the question, write C++ on the right, then Run or Check.
QuestionHint and solution stay closed until you open them
Read two positive integers and print their greatest common divisor.
std::gcd lives in <numeric> (C++17). Euclid's loop is fine too.
Input. Two integers a and b.
Output. One integer: gcd(a, b).
Constraints
- 1 ≤ a, b ≤ 1000000
Examples
Input
48 18
Output
6
Input
7 13
Output
1
Hint
- #include <numeric> then std::gcd(a, b).
- while (b) { int t = b; b = a % b; a = t; }
Show correct code
Peek only after you have tried. You can still Check your own version.
#include <iostream>
#include <numeric>
int main() {
int a, b;
std::cin >> a >> b;
std::cout << std::gcd(a, b) << "\n";
return 0;
}
main.cppC++17 · g++ · Ctrl + Enter runs
ResultIdle
Run to see output. Check grades the tests.