"Collatz Sequence"

i know that my problem is pretty simple to you guys !, , im noob , and i cant do it :( can u help me ???
heres d problem and my code :(

probleM:
"Collatz Sequence"
Take any natural number n.
If n is even, divide it by 2 to get n / 2.
If n is odd, multiply it by 3 and add 1 to get 3n + 1.
Repeat the process indefinitely.
In 1937, Lothar Collatz proposed that no matter what number you begin with, the sequence eventually reaches 1. This is widely believed to be true, but has never been formally proved.
Write a program that inputs a number from the user, and then displays the Collatz Sequence starting from that number. Stop when you reach 1.
Use while loop.



#include <iostream>
using namespace std;
main ()
{
int n ,ans;
cout <<" enter a number :";
cin>>n;

while (ans!=1)
{
ans=n;
if (n%0)
{
ans = (3*ans)+1;
cout<<ans<<" ";

}
else{
ans=n/2;
cout<<ans<<" ";
}


}
system ("pause");
}
n%0 What do you think it does?
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

int main ()
{
    int ans;
    
    std::cout << " Enter a number :";
    std::cin >> ans;

    while ( ans != 1 ){
        if ( ans % 2 == 1)
            ans = (3 * ans) + 1;
        else
            ans /= 2;
    
    std::cout << ans << " ";
    }
    
    return 0;
}
thanks for that sir ..
Topic archived. No new replies allowed.