C Program uses a for loop to print the powers of 2 table for the power 0 to 20, both positive and negative

				
					#include<stdio.h> 
#include<conio.h> 
void main() 
{ 
long int p; 
int n; 
double q; 
printf(“------------------------------------------------------------”); 
printf(“ 2 to power n      n        2 to power -n”); 
printf(“------------------------------------------------------------”); 
p=1; for(n=0; n<21; ++n) 
{ 
if(n==0) 
P=1; 
else 
p=p*2;
q=1.0/(double)p; 
} 
printf(“10ld 10%d %20.12lf”, p,n,q);
}
				
			
output:

--------------------------------------------------------------------------------------------

2 to power n                            n                          2 to power -n

----------------------------------------------------------------------------------------------


1
0
1.000000000000
2
1
0.500000000000
 
4
2
0.250000000000
8
3
0.125000000000
16
4
0.062500000000
32
5
0.031250000000
64
6
0.015625000000
128
7
0.007812500000
256
8
0.003906250000
512
9
0.001953125000
1024
10
0.000976562500
2048
11
0.000488281250
4096
12
0.000244140625
8192
13
0.000122070313
16384
14
0.000061035156
32768
15
0.000030517578
65536
16
0.000015258789
131072
17
0.000007629395
262144
18
0.000003814697
524288
19
0.000001907349
1048576
20
0.000000953674

Leave a Reply