Wednesday, November 7, 2012

Power of 2 with "for" loop and "if else" condition !!!!


public class PowerOf2 {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

int x;
long y =1;
System.out.println("X    This is called power of 2\n");

for(x =1; x <=10; x++){

if(x==1){
y = 1;
}
else{
y = y * 2;
}
System.out.println( x +  "         "+y);
}
}

}
___________________________________________

Output:

X    This is called power of 2

1         1
2         2
3         4
4         8
5         16
6         32
7         64
8         128
9         256
10       512
_____________________________________________________________

No comments:

Post a Comment