Computing Power of 2 using for loop

 In For loop, three actions are performed that is initialization, condition, Increment/decrement.
This is the main feature that are not found in any other loop.


class  fortest
{
public static void main(String aa[])
{
long p;
int n;
double q;
System.out.println("2 to power-n       n      2 to power n");
p=1;
for(n=0;n<10;++n)
{
if(n==0)
p=1;
else
p=p*2;
q=1.0/(double)p;
System.out.println("        "+q+"       "+n+"       "+p);
}
}

}

Output is....

power of 2 using for loop