Jan 17, 2013 at 4:58pm UTC
Didn't work, sorry. Results are the same.
Jan 17, 2013 at 5:03pm UTC
Lets say the input is 2.
while(2 > 1) //true
if(remainder of 2/2 == 0) //true
cout
input = 2 /2 //input now equals 1
if(remainder of 1/2 == 0) //false input equals 1 now, so there is a remainder
loop
while (1 > 1) //false; break
how about input = 3
while(3 > 1) //true continue
if(remainder of 3/2 == 0) //false continue
if(remainder of 3/2 == 0)// false continue
while(3 > 1) //true continue
this is a infinite loop. Any odd number is going to produce this, and any even number is going to loop only once.
I think what you want is:
while(input%i == 0)
then on the second if statement
if(!input%i == 0) i++; //increment the variable to an even number
Jan 17, 2013 at 5:25pm UTC
Try this:
1 2 3 4 5 6 7 8 9 10
while (input > 1)
{
if (input%i==0)
{
cout << input << "\t|\t" << i << endl;
input /= i;
continue ;
}
i++;
}
The
continue
at line 7 here means go back to the start of this
while
loop, hence
i
is not incremented, allowing for repeated factors.
Also, the value of
i
needs to be reset for each new user input.
Last edited on Jan 17, 2013 at 6:49pm UTC
Jan 17, 2013 at 5:34pm UTC
@pogrady Exactly! Thank you! The code did work as intended.
@Chervil I did as pogrady said, but your code works too so thanks.