i need a for loop to print out the numbers 1 2 4 8 16 32 64 128 256 512

any help?

Recommended Answers

All 4 Replies

got it, so simple after all

no thing but lol
before u post something work hard

OK friend, i am telling you the answer of this question. but it is so simple. Do little work before posting.

public static void main(String[] args) {
int initial = 1;
for(int i=0 ; i <= 10; i++){
System.out.println(initial);
initial = initial*2;
}
}

Hi;
May be the idea here is to understand how the binary numbers works;the numbers you print are all the power of 2.
you can do some thing lik:

public static void main(String[] args) {
        for (int i = 0; i <= 9; i++) {
            System.out.println(Math.pow(2, i));
        }
    }

if you did not want the digits after the point you can cast the result to int like this

public static void main(String[] args) {
        for (int i = 0; i <= 9; i++) {

            System.out.println((int)Math.pow(2, i));
        }
    }

or you can use the NumberFormat.

Hope it halps.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.