fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int findgcd(int a, int b){
  5. //continue loop as long as both a and b are greater then 0
  6. while(a>0 && b>0){
  7.  
  8. //update a to the remainder of a when divided by b
  9. if(a>b){
  10. a = a%b;
  11. }else{
  12. //else update b to the remainder of b divided by a
  13. b = b%a;
  14. }
  15. }
  16.  
  17. //check if a becomes 0, if so return b as the gcd
  18. if(a == 0){
  19. return b;
  20. }
  21.  
  22. return a;
  23.  
  24. }
  25.  
  26. int main() {
  27.  
  28. int n1, n2;
  29.  
  30. cin>>n1>>n2;
  31.  
  32. int gcd = findgcd(n1, n2);
  33.  
  34. cout<<gcd<<endl;
  35.  
  36. return 0;
  37. }
Success #stdin #stdout 0.01s 5288KB
stdin
20 15
stdout
5