loop for and pow() function

Dear C++ forum. I wish to make a series of numbers in a loop for, where each subsequent number equals 2 raised to the power of following natural numbers. I tried with this code, but something goes wrong. Any suggestions please.

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


using namespace std;


int main()
{
float n_bakt = 2.0; float i;
for (i=0; i<100; i++)
{
    //Sleep(1000);
    n_bakt = pow(n_bakt, i);
    cout<<n_bakt<<endl;
}

    return 0;
}


In the first iteration of the loop n_bakt will be set to 1. In the iterations that follows n_bakt will stay as 1 because pow(1, i) returns 1 for all values of i.
Last edited on
Easy fix:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include<iostream>
#include<windows.h>
#include<cmath>
using namespace std;
int main()
{double n_bakt=2,i;
for (i=0; i<100; i++){//Sleep(1000);
n_bakt=pow(n_bakt,i);
cout<<n_bakt<<endl;
if(n_bakt==1)
n_bakt++;
}
return 0;}
I got it!
thanks a lot!
Topic archived. No new replies allowed.