Thursday, 13 December 2012

COMPUTING THE ‘POWER OF 2’ USING FOR LOOP IN JAVA



COMPUTING THE ‘POWER OF 2’ USING FOR LOOP

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

No comments:

Post a Comment