Question: IN JAVA * * 15 /* 2 * This program calculates the powers of 2 from power = 0 to power = 20 and adds

IN JAVA

IN JAVA * * 15 /* 2 * This program calculates the

* * 15 /* 2 * This program calculates the powers of 2 from power = 0 to power = 20 and adds them together 3 * For example, to add all powers of 2 from power = 2 to power = 4 then the result is: 4 * 2^2 = 4 plus 2^3 = 8 plus 2^4 = 16 so the final result is: 4+8+16 = 28 5 6 7 */ 8 public class PrintAllPowers 9 106 public static void main(String[] args) 11 12 -Start below here. To do: approximate lines of code = 1 13 // Create an integer variable called sum and initialize it to O 14 15 --End here. Please do not remove this comment. Reminder: no changes outside the todo regions. 16 17 // -Start below here. To do: approximate lines of code = 2 18 // Write a for loop that counts from 0 to 20 (inclusive). Declare and use a variable called power as the loop variable 19 // Inside the loop use the Math.pow(double a, double b) function compute 2^power and add it to the sum variable 20 21 22 23 24 -End here. Please do not remove this comment. Reminder: no changes outside the todo regions. 25 System.out.println("Sum = " + sum ) ; 26 System.out.println("Expected: "); 27 System.out.println("Sum = " + 2097151) ; 28 29 30 11- -Start below here. To do: approximate lines of code = 6 31 // Reset sum variable to and then repeat the loop as above 32 // Inside the loop, instead of calling Math.pow, use an inner loop to calculate 2^power and add the result to the sum variable 33 34 35 36 37 38 39 40 41 42 43 1/ -End here. Please do not remove this comment. Reminder: no changes outside the todo regions. 44 System.out.println("Sum = " + sum); 45 System.out.println("Expected: "); 46 System.out.println("Sum = " + 2097151); 47 } 48 }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!