# Thread: Need some help with a simple program

1. ## Need some help with a simple program

Well...I am not sure if this is the mistake the author makes before the book is published or I just misinterpret it. Anyway, here is the example of the very simple program:

Code:
```#include <iostream>

using namespace std;

int main() {
int i, n;

//  Get a number from the keyboard and initialize i.

cout << "Enter a number and press Enter: ";
cin >> n;
i = 1;
while (i <= 1){                    // While i less than or equal to n,
cout << i << " ";           //   Print i
i = i + 1;                        //   Add 1 to i.
}

return 0;
}```
And then here comes the question for the exercise
2.2.2 Alter the example so it prints all the numbers from n to 1 in reverse order. For example: 5 4 3 2 1. (Hint: to decrement a value inside the loop use the statement i = i - 1.

I get stuck here. I was unable to print n to 1 with the use of i = i - 1. Maybe because of my low level math skills. This is my code:

Code:
```#include <iostream>

using namespace std;

int main()
{
// Declare two variables as n and i
int n, i;

// Assign 5 to i and 1 to n and do the calculation
n = 1;
i = 5;
while(i >= n){
cout << i << " ";
i = i - 1;
}

return 0;
}```
I can't have the cout print n to 1, only can I have cout print i. But if I wanna print n, I cannot use i = i - 1 expression. Is it a mistake of the exercise or I misinterpret it? I dunno how I can print i with the use of i = i - 1. It seems impossible to me.

2. Um, your code actually looks correct if you want to print "5 4 3 2 1 ".

EDIT:
Okay, I think I understand: you are confused because you want to start from n, but this appears to start from i. The trick is that n is just user input. You know that starting from i = 5 is correct, so now you just need to substitute it with n:
Code:
`i = n;`
Since you know that you are printing from n to 1, use 1 in the loop condition:
Code:
`while(i >= 1){`

3. That sounds so damn stupid an exercise...There is no point in doing that...I don't know what the author really want the reader or the user to do with the program...

Anyway, that's the trick has really helped me...but this damn exercise has really put me in a fix...I have been bothered by this for a while and didn't really realize that to replace i = n and use 1 => i

4. Code:
```#include <iostream>

using namespace std;

int main()
{
// Declare two variables as n and i
int n, i;

// Assign 5 to i and 1 to n and do the calculation
i = 5;

while(i >= 1){
n = i;
cout << n << " ";
i = i - 1;
}

return 0;
}```
Is it what you meant?

5. No, I mean something like your book's original example:
Code:
```#include <iostream>

using namespace std;

int main() {
int i, n;

//  Get a number from the keyboard and initialize n.
cout << "Enter a number and press Enter: ";
cin >> n;

i = n;
while (i >= 1) {      // While i greater than or equal to 1
cout << i << " "; // Print i
i = i - 1;        // Subtract 1 from i.
}

return 0;
}```
That said, note that the comments are unnecessary in this case: I merely altered the original comments so as to explain what happens step by step, but any programmer past the novice stage should be able to understand that code without comments.

6. Got cha. Yeah I think I have misinterpreted the question...And now I understand.