#include<iostream>
using namespace std;
int main(){
int a= 12;
int b=3;
int c=a;
int d=b;
for (int i=1; i<b; i++){
if (c%i==0&&d%i==0){
do {(c=c/i)&&(d=d/i);}
while ((c%i!=0)&&(d/i!=0));
}
}
cout << c << "/" << d;
return 0;
}
Did I forget & values?
I am getting 12/3 result, instead of expected (wanted) 4/1
//
#include<iostream>
usingnamespace std;
int main(){
int a= 10;
int b=6;
int e;
if (a<b){e=a;}
else
{e=b;}
for (int i=1; i<=e; i++){
if ((a%i==0)&&(b%i==0)){
do {(a/=i)&&(b/=i);}
while ((a%i!=0)||(b/i!=0));
}
}
cout << a << "/" << b;
return 0;
}
#include<iostream>
usingnamespace std;
int main(){
int a= 10;
int b=6;
int e;
if (a<b){e=a;}
else
{e=b;}
for (int i=1; i<=e; i++){
if ((a%i==0)&&(b%i==0)){
do {(a/=i)&&(b/=i);}
while ((a%i!=0)||(b/i!=0));
}
}
cout << a << "/" << b;
return 0;
}
Yeah... the thread last night dove headfirst into derpy territory.
Another thing, a and b are awful variables names unless you're doing something like the Pythagorean Theorem or something else where 'a' and 'b' have some widely understood meaning. Longer variable names don't slow down your program, they speed up your comprehension!
#include<iostream>
usingnamespace std;
int hcf( int num1, int num2 );
int main()
{
int b = 2022161;
int a = 3065857;
//--- Save values for second version
int numer = a;
int denom = b;
//--- Debugged version of "enemy" code
int &e = (a < b) ? a : b;
for (int i=2; i<=e; i++)
while ((a%i==0) && (b%i==0))
{
a /= i;
b /= i;
}
cout << "One: " << a << " / " << b << endl;
//--- Alternative method
int factor = hcf( numer, denom );
numer /= factor;
denom /= factor;
cout << "Two: " << numer << " / " << denom << endl;
return 0;
}
// recursive function for Highest Common Factor:
int hcf( int num1, int num2 )
{
if (num2 == 0)
return num1;
elsereturn hcf(num2, num1 % num2);
}
#include<iostream>
usingnamespace std;
int main()
{
#include<iostream>
usingnamespace std;
int main()
{
int b = 2*2*2*3*3*3*3*3*5*5;
int a = 5*2*2;
//--- Save values for second version
int numer = a;
int denom = b;
//--- Debugged version of "enemy" code
int &e = (a < b) ? a : b;
for (int i=2; i<=e; i++)
while ((a%i==0) && (b%i==0))
{
a /= i;
cout <<b<<endl;
b /= i;
cout <<b<<endl;
}
cout << "One: " << a << " / " << b << endl;
return 0;
}
Output:
48600
24300
24300
12150
12150
2430
One: 1 / 2430
Question: can someone tell me, step by step, what is acutally happenng in the loop!!
Why is the second number written twice?
Many thanks!
#include<iostream>
usingnamespace std;
int main()
{
int b = 1000;
int a = 25;
//--- Save values for second version
int numer = a;
int denom = b;
//--- Debugged version of "enemy" code
int &e = (a < b) ? a : b;
for (int i=2; i<=e; i++)
while ((a%i==0) && (b%i==0))
{
a /= i;
cout <<b<<endl;
b /= i;
cout <<b<<endl;
}
cout << "One: " << a << " / " << b << endl;
return 0;
}
Output:
1000
200
200
40
One: 1 / 40
i=2 - while is inactivated
i=3 while is inactivated
i=4 while is inactivated
i=5 while starts to act:
1. b=1000 because it is still not coming to the next line sayig that b is gonna change.
2. b=200 this is now the first dividing with i=5, but, IT IS STILL LINE (23) isn't it?
#include<iostream>
usingnamespace std;
int main()
{
int b = 2000;
int a = 25;
//--- Save values for second version
int numer = a;
int denom = b;
//--- Debugged version of "enemy" code
int &e = (a < b) ? a : b;
for (int i=2; i<=e; i++)
while ((a%i==0) && (b%i==0))
{
a /= i;
cout <<b<<endl;
b /= i;
}
cout << "One: " << a << " / " << b << endl;
return 0;
}