Answered step by step
Verified Expert Solution
Question
1 Approved Answer
IN JAVA * * 15 /* 2 * This program calculates the powers of 2 from power = 0 to power = 20 and adds
IN JAVA
* * 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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started