I have this problem... Ive gotten the codes however, i cant seems to get the output correctly..

This is my program:

int n;
cin>>n; //reading in value of n

if (n==1)
{
cout<<n; //if n=1, print out 1
}

else if (n%2)
{
n = 3*n + 1; //if n is an odd number, print out n = 3*n + 1
}

else
{
n = n/2; //or else print n = n/2
}

cout<<n; //printing the number n

The result that I have gotten when i type in 22 is 11, which is correct.. However, Im supposed to display this result
-> 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1

I tried putting in a for loop -> for (int n=0;n<1000000;n++) but it doesnt work... I coulnt get any output at all when i add the for loop.. the problem states that integer n has value from 0<n<1000000.. what should i do to correct this problem.. So sorry, Im new in this..

22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1

wats this
how u expect this ?

I tried putting in a for loop -> for (int n=0;n<1000000;n++) but it doesnt work.

Thats because you modify the control variable used in the for loop inside your for loop i.e. you use the variable n to control variable but modify the value of n inside the loop.

Try declaring a new variable outside the loop which will hold of the current value under consideration. Also either read the numbers from a file or an array declared in your program.

Variable naming is very important because if you use names to short, you will end up with various ambiguities in your project like the loop in this case. Make it too long, and then you end up typing far more than you need to.

As a general rule, I like to reserve index iterators as 'i' or 'index', or x, y, and z for nested loops. I then use abbreviations for the rest of the variables in my project. Namespaces are another useful tool to prevent name abiguities.

22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1

wats this
how u expect this ?

this is expected when i input in 22, the result will divide by 2 which is 11.
after which 11 is an odd number, and hence the result is 3*11+1 which is 34.
34 is an even number, hence again 34/2 which is 17 and this goes on till i reach the number one, where the program will stop.

I have this almost figured out but for some reason my output stops at 5. I get

11 34 17 52 26 13 40 20 10 5 5

this is the code I entered:

#include<iostream>
usingnamespace std;

int main()
{

int number;
cout << "Enter value: ";
cin >> number;

for (int x = 0; x <= number; x++)
{

if (number==1)
cout << number;
else if (number % 2==0)
number = number/2;
else
number = number*3 + 1;
cout << number << endl;
}

cout << number << endl;
return 0;
}
\int main()
{

int number;
cout << "Enter value: ";
cin >> number;
[B]int n = number;[/B]
[B]for (int x = 0; x <= n,number !=1; x++)[/B]
{

if (number==1)
cout << number;
else if (number % 2==0)
number = number/2;
else
number = number*3 + 1;
cout << number << endl;
}

cout << number << endl;
return 0;
}

I have this almost figured out but for some reason my output stops at 5. I get

11 34 17 52 26 13 40 20 10 5 5

this is the code I entered:

#include<iostream>
usingnamespace std;

int main()
{

int number;
cout << "Enter value: ";
cin >> number;

for (int x = 0; x <= number; x++)
{

if (number==1)
cout << number;
else if (number % 2==0)
number = number/2;
else
number = number*3 + 1;
cout << number << endl;
}

cout << number << endl;
return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.