Java program – Computing powers of two

by Nideesh C on April 28, 2011 · 0 comments

in Java Programming




public class PowersOfTwo {
    public static void main(String[] args) {

        // read in one command-line argument
        int N = Integer.parseInt(args[0]);

        int i = 0;                // count from 0 to N-1
        int powerOfTwo = 1;       // the ith power of two

        // repeat until i equals N
        while (i <= N) {
            System.out.println(i + " " + powerOfTwo);   // print out the power of two
            powerOfTwo = 2 * powerOfTwo;                // double to get the next one
            i = i + 1;
        }

    }
}


/*************************************************************************
 *  This program takes a command-line argument N and prnts a table of
 *  the powers of 2 that are less than or equal to 2^N.
 *
 *  % java PowersOfTwo 5
 *  0 1
 *  1 2
 *  2 4
 *  3 8
 *  4 16
 *  5 32
 *
 *  % java PowersOfTwo 6
 *  0 1
 *  1 2
 *  2 4
 *  3 8
 *  4 16
 *  5 32
 *  6 64
 *
 *  Remarks
 *  ------------
 *  Only works if 0 <= N < 31 since 2^31 overflows an int.
 *
 *************************************************************************/



Not Satisfied ? Just search & get the result

Related Posts Plugin for WordPress, Blogger...
Be Sociable, Share!

Related posts:

  1. Java program – Casting to get a random integer
  2. Java program – Integer multiplication and division
  3. Java program – Check Leap year
  4. Java program – Quadratic formula
  5. Java program – While loop

Leave a Comment

Previous post:

Next post: