this code should calculate the greates common divisor of 2 integers entered by the user.
Both the recursive & ilterative function should have the same result but they don't.
I can't for the life of me figure out what im doing wrong.
Any help would be much appreciated !
Code:
#include <stdio.h>
int gcd(int a,int b);
int gcdliterative(int a,int b);
void main(){
int a,b,c,d;
printf("Enter two posetive integers,\n");
printf("that you'd like to see the greatest common divisor off\n");
if ((scanf("%d%d",&a,&b)) != 2){
printf("Error!You needed to input 2 posetive integers, exiting...\n");
exit(1);
}
c = gcd(a,b);
printf("The greatest common divisor off:%d and %d is: %d\n",a,b,c);
printf("Calculating again ,useing a literative loop this time\n");
d = gcdliterative(a,b);
printf("The greatest common divisor off:%d and %d is: %d\n",a,b,d);
}
int gcd(int a,int b){
int r;
if ((r = a % b) == 0)
return b;
else
return gcd(b,r);
}
int gcdliterative(int a,int b){
int r;
while((r = a % b) == 1){
a = b;
b = r;
}
return r;
}