0% found this document useful (0 votes)
21 views

Exercise 2 - Operators Control Statements

The document contains 8 exercises involving Java operators and control statements. The exercises include using conditionals to check values, loops, logical operators, and modulus. Test cases are provided for some exercises.

Uploaded by

Urjoshi Aich
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Exercise 2 - Operators Control Statements

The document contains 8 exercises involving Java operators and control statements. The exercises include using conditionals to check values, loops, logical operators, and modulus. Test cases are provided for some exercises.

Uploaded by

Urjoshi Aich
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Exercise 2 – Operators & Control Statements

1. Write a Java program that reads a floating-point number and prints "zero" if the number
is zero. Otherwise, print "positive" or "negative". Add "small" if the absolute value of
the number is less than 1, or "large" if it exceeds 1,000,000. (2)

Condition: Use Math.abs() to check absolute value

Test cases:

1. Input a number greater than 0


2. Input a number equal to 0
3. Input a number less than 0
4. Input a number greater than 1,000,000

2. Write a Java program that reads in two floating-point numbers and tests whether they
are the same for three decimal places (2)

Condition: Use Math.round()

3. Write a program to get 5 inputs from user and find the sum and average (1)

Condition: Use for loop & Compound assignment operator

4. Write a Java program that accepts three numbers and prints "All numbers are equal" if
all three numbers are equal, "All numbers are different" if all three numbers are
different and "Neither all are equal or different" otherwise (1)

Condition: Use Logical operators

5. Write a Java program that keeps a number from the user and generates an integer
between 1 and 7 and displays the name of the weekday (1)

Condition: Use Switch case


6. Consider the following code (1)

int i = 10;

int n = i++%5;

a. What are the values of i and n after the code is executed?


b. What are the final value of i and n if instead of using the postfix operator than the
prefix operator for variable i?

7. Consider the following code (1)

if (aNumber >= 0)
if(aNumber == 0)
System.out.println(“First String”);
else System.out.println(“Second String”);
System.out.println(“Third String”);

a. What output do you think the code will produce if aNumber is 3?


b. Use braces, { and } to clarify the loop

8. Write a program to calculate the sum of the first and the second last digit of 5 digits.
(1)

Condition: Use modulus operator

E.g - Number: 12345 Output: 1+4 = 5

You might also like