/* Lab : Lab3 Day : Tuesday [ 12 Aug 08 ] Problem Number: 02 Problem Title : Exact power of 2 Theory : when you successively multiply 2 to 1 you would generate exact powers of 2 The problem requires a while loop that will successively generate exact powers of 2 and with every exact power, compare the num Ther can be three cases 1. so far generated power of 2 and num are same Than print " Number is exact power of 2" and end the program 2. num is less than so far generated powre of 2 ' Than print " Number is not exact power of 2" and end the program 3. so far generated power of 2 ( number obtained by successively multiplying 2 to 1 ) becomes negative Then also print " Number is not exact power of 2" and end the program because overflow had occured meaning num is not exact power of 2 */ class sol_lab3_prob02{ public static void main(String args[]) { // Declare a variable num of type int and assign it some positive value int num; num = 128; // done // create some more variable int i = 0; // an integer that represents what is current power that is raised on 2 int n = 1; // an integer that represents value whan 2 is raised power i while(true) { if( n == num ) { System.out.println("[ Number is exact power of 2] it is 2^" + i); //print message return; // and exit from program } if( n > num ) { System.out.println(" Number is NOT exact power of 2"); //print message return; // and exit from program } if( n <= 0 ) { System.out.println(" Number is NOT exact power of 2 : OVERFLOW"); //print message return; // and exit from program } i = i + 1; n = n * 2; // create next power of 2 for testing } } }