I am trying to figure out how to make a program that would output hailstone numbers.
The criteria is:
If the number is even, divide it in half
If the number is odd, multiply it by 3 then add 1
If the number reaches 1, stop.
So far, I have this:
if(Num % 2 == 0)
{
while(Num % 2 ==0)
{
cout << Num << endl;
Num = Num / 2;
}
cout << Num << endl;
}
if(Num % 2 == 1)
{
while(Num % 2 == 1)
{
Num = (Num * 3) + 1;
}
cout << Num << endl;
}
How do I make it so that the program will end if one of the numbers turns out to be 1?
Thanks.
2007-01-24
03:37:08
·
2 answers
·
asked by
Defcon6
2
in
Computers & Internet
➔ Programming & Design
Nevermind, I was able to get the program to stop if/when the output goes to 1.
However, how do I make it so that the if statements will repeat if the output is an even or odd number?
2007-01-24
03:47:19 ·
update #1