#include<iostream>
#include<math.h>
usingnamespace std;
int n;
int d;
int a;
int b;
int gcd;
int main(){
cout<<"Please enter numerator:"<<endl;
cin>>a;
cout<<endl;
cout<<"Please enter denominator:"<<endl;
cin>>b;
cout<<endl;
while (b != 0) {
int gcd = a % b;
a = b;
b = gcd;
}
return a;
cout<<"the reduced fraction is:"<<endl
<<a/gcd<<"/"<<b/gcd;
system("pause");
return 0;
}
#include<iostream>
#include<math.h>
usingnamespace std;
int n;
int d;
int a;
int b;
int gcd;
int main(){
cout<<"Please enter numerator:"<<endl;
cin>>a;
cout<<endl;
cout<<"Please enter denominator:"<<endl;
cin>>b;
cout<<endl;
while (b != 0) {
int gcd = a % b;
a = b;
b = gcd;
}
return a;// <----------- this is where your problem is
cout<<"the reduced fraction is:"<<endl
<<a/gcd<<"/"<<b/gcd;
system("pause");
return 0;
}
You are closing the function as soon as you return 0, so simply get rid of that line and you should be fine
okay, i got that, when i run it, it always goes "the reduced fraction is 1/1" i enter for numerator 2491 denominator 3127, it should end with 47/59 with the gcd=53
On line 23 'int gcd' hides that one on line 10. Means: the 'gcd' variable outside the while loop will never be changed and remains uninitialized. Remove the int on line 23, but then gdc will be 0 at the end. Take care that the 0 will not be assigned to gcd
I'm going to guess that you just copy-paste that snip of code. And that that was inside a function.
¿Haven't you realized that you are modifying 'a' and 'b' ?
At the end of the loop, 'b' will be zero and 'a' will be the greatest common divisor that you were looking for. But you lost the original values.
// problem 1.cpp : Defines the entry point for the console application.
//
#include<iostream>
#include<math.h>
usingnamespace std;
int n;
int d;
int a;
int b;
int gcd;
int main(){
cout<<"Please enter numerator:"<<endl;
cin>>n;
cout<<endl;
cout<<"Please enter denominator:"<<endl;
cin>>d;
cout<<endl;
a=n;
b=d;
a % b;
a = b;
b = gcd;
cout<<"the reduced fraction is:"<<endl
<<n/gcd<<"/"<<d/gcd;
return 0;
}
// problem 1.cpp : Defines the entry point for the console application.
//
#include<iostream>
#include<math.h>
usingnamespace std;
int n;
int d;
int a;
int b;
int gcd;
int main(){
cout<<"Please enter numerator:"<<endl;
cin>>n;
cout<<endl;
cout<<"Please enter denominator:"<<endl;
cin>>d;
cout<<endl;
a=n;
b=d;
while (gcd!=0) { gcd = a % b; a = b; b = gcd; }
cout<<"the reduced fraction is:"<<endl
<<n/gcd<<"/"<<d/gcd;
return 0;
}