(Multiples of 2 with an Infinite Loop) Write a program that prints the powers of the integer 2, namely 2, 4, 8, 1 6, 32, 64, etc.
Your while loop should not terminate (i.e., you should create an infinite loop). To do this, simply use the keyword true as the expression for the while statement. What happens when you run this program?
Solution:
#include
#include
#include
using namespace std;
int main()
{
int x = 2;
while( x > 0 )
{c
out << x << endl;
x *= 2;
}/
/for pause
system("PAUSE") ;
return 0;
}
4
8
16
32
64
128
256
512
1024
2048
4096
8192
16384
32768
65536
131072
262144
524288
1048576
2097152
4194304
8388608
16777216
33554432
67108864
134217728
268435456
536870912
1073741824
Your while loop should not terminate (i.e., you should create an infinite loop). To do this, simply use the keyword true as the expression for the while statement. What happens when you run this program?
Solution:
#include
#include
#include
using namespace std;
int main()
{
int x = 2;
while( x > 0 )
{c
out << x << endl;
x *= 2;
}/
/for pause
system("PAUSE") ;
return 0;
}
Result:
24
8
16
32
64
128
256
512
1024
2048
4096
8192
16384
32768
65536
131072
262144
524288
1048576
2097152
4194304
8388608
16777216
33554432
67108864
134217728
268435456
536870912
1073741824
Post A Comment:
0 comments: