🎉 Celebrating 25 Years of GameDev.net! 🎉
Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!
int numerator, denomenator; // set these to something
int smallest; // holds the smaller of the two variables
int gcf; // greatest common factor
// determine which number is smallest, doesn''t matter if equal
if ( numerator >= denomenator )
{ smallest = numerator; }
else
{ smallest = denomenator; }
for ( int i = smallest; i > 0; i++ )
{
// if both numbers divide evenly, then it is divisible by i
if ( ( numerator % i == 0 ) && ( denomenator % i == 0 ))
{ gcf = i; }
}
it might help a bit if you add:
cout << gcf;
also, you need to name int i earlier in your coding. putting it in the middle of the program slows the program down. 1 more thing, why do you have:
if ( numerator >= denomenator )
{ smallest = numerator; }
else
{ smallest = denomenator; }
shouldn''t it be:
if ( numerator <= denomenator )
{ smallest = numerator; }
else
{ smallest = denomenator; }
int smallest; // holds the smaller of the two variables
int gcf; // greatest common factor
// determine which number is smallest, doesn''t matter if equal
if ( numerator >= denomenator )
{ smallest = numerator; }
else
{ smallest = denomenator; }
for ( int i = smallest; i > 0; i++ )
{
// if both numbers divide evenly, then it is divisible by i
if ( ( numerator % i == 0 ) && ( denomenator % i == 0 ))
{ gcf = i; }
}
it might help a bit if you add:
cout << gcf;
also, you need to name int i earlier in your coding. putting it in the middle of the program slows the program down. 1 more thing, why do you have:
if ( numerator >= denomenator )
{ smallest = numerator; }
else
{ smallest = denomenator; }
shouldn''t it be:
if ( numerator <= denomenator )
{ smallest = numerator; }
else
{ smallest = denomenator; }
declaring varibles in the middle of the code vs at the top does not slow anything down at all. whoever told you that lie is quite ignorent or does not understand the mechanics of a compiler.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement