I dont understand for statements

I need help making for statements. I just wanted to mess around and make a program where you would enter a number and a for statement would produce something like this:

Enter a number: 9

1-1
2-2
3-4
4-8
5-16
6-32
7-64
8-128
9-246

here is what i have so far
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  #include <iostream>

using namespace std;

int main()
{
int num;
double a;

a = 1;

cout << "Enter a number: " << endl;
cin >> num;

for (num < 100)
{
a = a * 2;
 cout << a << endl;
}

system ("Pause");
return 0;
}
I also want to do one with factorials like 6! ( 6 * 5 * 4 * 3 * 2 *1)
but I dont know where to start
Please read: http://www.cplusplus.com/doc/tutorial/control/
Section Iteration statements (loops)

for (num < 100) { /*stuff*/ } is not a valid for loop.
while (num < 100) { /*stuff*/ } would be valid syntax, and would be read out loud as "while num is less than 100, do stuff (and loop)"

But even if you had a while loop, the problem is that you don't ever change the value of num inside the loop. Upon entering the loop, num will always be less than 100, because there is no way to make it greater than 100.

But... forget about the number 100. That has nothing to do with your input or your output.

There are different options here, but one way is to change the for loop to a while loop, and make it something like this

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

using namespace std;

int main()
{
    int num;
    double a;
    a = 1;

    cout << "Enter a number: " << endl;
    cin >> num;

    while (num > 1)
    {
        a = a * 2;
        num--;
    }
    cout << a << endl;

    return 0;
}


Now try to do the factorial in a similar way.
Last edited on
Thank You!
Topic archived. No new replies allowed.