Skip to content

Commit

Permalink
6.31 gcd
Browse files Browse the repository at this point in the history
  • Loading branch information
PureJoyMind committed Apr 4, 2022
1 parent d432172 commit c02676a
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions ch.06/gcd.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Deitel ch.06 exercise 6.31
// Finding the gcd of a number without recursion

#include <iostream>

using namespace std;

int gcd(int, int);

int main(){
int a{76};
int b{92};
cout << "The gcd of "<< a << " and " << b << " is: " << gcd(a, b) << endl;
}

int gcd(int a, int b){
if(b > a){
int tmp;
tmp = b;
b = a;
a = tmp;
}

int gcd{0};

for(int i{1}; i <= b; i++){
if((a % i == 0) && (b % i == 0)){
gcd = i;
}
}

return gcd;
}

0 comments on commit c02676a

Please sign in to comment.