Very simple program not behaving as expected.

Here is a tiny simple little program I created as an excersise from the book C++ Without Fear.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <cstdlib>
#include <iostream>

using namespace std;

int main()
{
    int i = 0, n;
    
    cout << "Enter a number: ";
    cin >> n;
    
    while (i <= n)
    {
          if ((i%2)==0)
          {
             cout << i << " ";
             i = i + 1;
          }
    }
    
    system("PAUSE");
    return EXIT_SUCCESS;
}


It is supposed to take a number from the user (n) and show all the even numbers leading up to n from 0. All I see is one single 0 and the program gets stuck.

What have I done wrong?
Last edited on
Never mind I realised I have the incrementation in the wrong block :)
Topic archived. No new replies allowed.