Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

(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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Relational Database And Transact SQL

Authors: Lucy Scott

1st Edition

1974679985, 978-1974679980

More Books

Students also viewed these Databases questions

Question

3. What changes should I be making?

Answered: 1 week ago

Question

2. Why?

Answered: 1 week ago