Consider the following process, which can be applied to any positive integer: if the integer is odd, multiply it by 3 and add 1; if the integer is even, divide it by 2. This process is repeated until the integer remaining is 1. As an example, consider 34, the process terminates in 13 steps, producing the following sequence: 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1. The process stops when 1 occurs. Write a program that receives an integer from a user and determines the number of steps it takes for termination and the largest intermediate value generated ( 13 and 52 for the example respectively).
my code was like that :
CODE
#include <iostream>
using namespace std;
int main()
{
int num;
int steps = 0;
cout << "Please enter a number: ";
cin >> num;
while(num >1)
{
cout << num << " ";
if((num % 2) != 0)
{
num *= 3;
num++;
}
else
num /= 2;
++steps;
}
cout << num << endl;
cout << "Number of steps: " << steps << endl;
system("PAUSE");
return 0;
}