Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

You\'ve been hired by Number Knights to write a Java console application that in

ID: 3789078 • Letter: Y

Question

You've been hired by Number Knights to write a Java console application that investigates how well Java handles large integers. Write two loops that iterate from 0 through 35. Before each loop, set an integer variable (IV) to 1. Within each loop, print the loop count and the value of IV formatted in two columns. Within the first loop, multiply IV by 2. Within the second loop, multiply IV by the appropriate StrictMath method. The second loop will not complete since there will eventually be an integer overflow.

Explanation / Answer

public class Test{
   public static void main(String args[]){
       int iv = 1;
       for(int i = 0; i <= 35; i++){
           System.out.printf("%d %d ", i, iv);
           iv *= 2;
       }
       System.out.println();
       iv = 1;
       for(int i = 0; i <= 35; i++){
           System.out.printf("%d %d ", i, iv);
           iv = (int) StrictMath.pow(2, i);
       }
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote