Menu

[solved]-Write Java Program Case Countdown Prove Loop Terminates N Positive General Easy Tell Wheth Q39061438

Write a Java program

In the case of countdown(), we can prove that the loopterminates when n is positive. But in general, it is notso easy to tell whether a loop terminates. For example, this loopcontinues until n is 1 (which makes the conditionfalse).

Write the method sequence() into a program/class andrun it for sequence( 3 ), sequence( 16 ), sequence( 27 ) (111steps).

In the case of countdown, we can prove that the loop terminateswhen n is positive. But in general, it is not so easy to tellwhether a loop terminates. For example, this loop continues until nis 1 (which makes the condition false):

public static void sequence(int n) { while (n != 1) { System.out.println(n); if (n % 2 == 0) { // n is even n = n / 2; } else { // n is odd

} }

n = n * 3 + 1; }

Expert Answer


Answer to Write a Java program In the case of countdown(), we can prove that the loop terminates when n is positive. But in genera… . . .

OR


Leave a Reply

Your email address will not be published. Required fields are marked *