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
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