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);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.