0% found this document useful (0 votes)
21 views2 pages

Java Conditional Statement Exercises

Uploaded by

honey macagubang
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views2 pages

Java Conditional Statement Exercises

Uploaded by

honey macagubang
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Java Conditional Statement : Exercises

[Link]
1. Write a Java program to get a number from the user and print whether it is positive or negative.
Test Data
Input number: 35
Expected Output :
Number is positive

2. Write a Java program to solve quadratic equations (use if, else if and else).
Test Data
Input a: 1
Input b: 5
Input c: 1
Expected Output :
The roots are -0.20871215252208009 and -4.7912878474779195

3. Write a Java program that takes three numbers from the user and prints the greatest number.
Test Data
Input the 1st number: 25
Input the 2nd number: 78
Input the 3rd number: 87
Expected Output :
The greatest: 87

4. 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.
Test Data
Input a number: 25
Expected Output :
Input value: 25
Positive number

5. Write a Java program that takes a number from the user and generates an integer between 1 and 7. It displays the weekday name.
Test Data
Input number: 3
Expected Output :
Wednesday

6. Write a Java program that reads two floating-point numbers and tests whether they are the same up to three decimal places.
Test Data
Input floating-point number: 25.586
Input floating-point another number: 25.589
Expected Output :
They are different

7. Write a Java program to find the number of days in a month.


Test Data
Input a month number: 2
Input a year: 2016
Expected Output :
February 2016 has 29 days

8. Write a Java program that requires the user to enter a single character from the alphabet. Print Vowel or Consonant, depending on
user input. If the user input is not a letter (between a and z or A and Z), or is a string of length > 1, print an error message.
Test Data
Input an alphabet: p
Expected Output :
Input letter is Consonant

9. Write a Java program that takes a year from the user and prints whether it is a leap year or not.
Test Data
Input the year: 2016
Expected Output :
2016 is a leap year

10. Write a Java program to display the first 10 natural numbers.


Expected Output :
The first 10 natural numbers are:
1
2
3
4
5
6
7
8
9
10
Answer key

1.
import [Link];
public class Exercise1 {

public static void main(String[] args)


{
Scanner in = new Scanner([Link]);
[Link]("Input number: ");
int input = [Link]();

if (input > 0)
{
[Link]("Number is positive");
}
else if (input < 0)
{
[Link]("Number is negative");
}
else
{
[Link]("Number is zero");
}
}
}

2output: Sample Output:


Input number: 35
Number is positive

Common questions

Powered by AI

Java determines the greatest of three numbers by comparing them using nested conditional statements. It first compares the first two numbers, then the result with the third number, ultimately printing the greatest of the three .

A Java program identifies positive and negative numbers using conditional statements by checking if the input number is greater than, less than, or equal to zero. It prints 'positive' if the number is greater than zero, 'negative' if it is less than zero, and 'zero' if it equals zero .

Java determines the number of days in a month using conditional statements to check for month input and special rules for February. It considers leap years by checking divisibility conditions on the year (using 4, 100, and 400) to decide February's days .

Java determines a leap year if the year is divisible by 4, but not by 100 unless also divisible by 400. Conditional statements implement these checks to conclude if the year is a leap year .

A Java program maps integers (1 to 7) to weekday names using conditional statements or switch cases, where each integer corresponds to a specific weekday, starting from Sunday to Saturday .

Java compares two floating-point numbers for up to three decimal places by truncating or rounding the numbers to three decimal places and checking if they are equal using conditional statements .

Java uses a simple loop structure to iterate from one to ten, printing each number in the sequence of the first ten natural numbers .

To solve quadratic equations using conditional statements in Java, the program first calculates the discriminant (b² - 4ac). It uses 'if', 'else if', and 'else' statements to check the value of this discriminant. If the discriminant is positive, the program computes two real roots. If zero, it computes one real root. If negative, it indicates no real roots exist .

A Java program validates user input to correctly determine if an input is a vowel or consonant to avoid errors when non-alphabet or multicharacter strings are entered. Input is checked for character bounds and length to ensure it meets criteria .

Java uses conditional statements to evaluate a floating-point number. It checks if the number equals zero, is positive, or is negative. For non-zero numbers, it adds 'small' if the absolute value is less than 1, or 'large' for values exceeding 1,000,000 .

You might also like