Question: (IN JAVA) Q1. (a) Explain why an odd number is generated by the following program segment, no matter what the value of anyInteger. Assume that

(IN JAVA)

Q1. (a) Explain why an odd number is generated by the following program segment, no matter what the value of anyInteger. Assume that all the variables are declared to have data type int.

anyInteger = sc.nextInt();

half = anyInteger / 2;

oddNumber = half * 2 + 1;

(b) Write a program which reads an arbitrary integer. It then prints the integer if it is even; otherwise it prints the number one less than the integer. For example, If 10 is read, 10 is printed because it is even, but if 9 is read, 8 is printed, since 8 is an even number one less than 9.

(c) Write a program which reads an arbitrary integer. It then prints the closest number divisible by three which is equal to or greater than the original integer. For example, if 10 is read, 12 is printed, since it is the next highest integer divisible by 3. If 15 is read, then 15 is printed, since it is divisible by 3.

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!