Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

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
_____________________________________________________________



This post first appeared on LET's START CORE JAVA !, please read the originial post: here

Share the post

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

×

Subscribe to Let's Start Core Java !

Get updates delivered right to your inbox!

Thank you for your subscription

×