Skip to the content.

Java > πŸ§‘β€πŸ’» Conditional Statements Exercise

Problem 1: Simple If Condition

Description: Write a Java program that checks if a number is positive using a simple if statement.

Example:

Input: Enter an integer number from the user
Output: 10 is a positive number

Problem 2: If-Else Condition

Description: Write a Java program to check if a given number is even or odd using an if-else statement.

Example:

Input: Declare an integer variable num with value 25
Output: 25 is an odd number

Problem 3: If-Else Ladder Condition

Description: Write a Java program that determines the grade of a student based on their marks using an if-else ladder.

Example:

Input: Take marks input from the user
Output: Grade: A

Problem 4: Nested If-Else Condition

Description: Write a Java program to find the largest among three numbers using nested if-else.

Example:

Input: Declare three integer variables a = 12, b = 25, c = 7
Output: Largest number: 25

Problem 5: Switch-Case Statement

Description: Write a Java program that takes a number (1-7) and prints the corresponding day of the week using switch-case.

Example:

Input: Declare an integer variable day with value 3
Output: Wednesday

Problem 6: Voting Eligibility Checker

Description: Write a Java program to check if a person is eligible to vote (age >= 18) using an if-else statement.

Example:

Input: Take age input from the user
Output: Eligible to vote

Problem 7: Traffic Light Simulator

Description: Write a Java program that simulates traffic light behavior using switch-case. The user inputs a color (red, yellow, green) and the program prints the corresponding action.

Example:

Input: Take traffic light color input from the user
Output: Stop

Problem 8: Leap Year Checker

Description: Write a Java program that determines whether a given year is a leap year using an if-else statement.

Example:

Input: Take year input from the user
Output: 2024 is a leap year

Problem 9: Discount Calculation

Description: Write a Java program that calculates a discount based on a purchase amount. If the amount is above 500, apply a 10% discount; otherwise, no discount.

Example:

Input: Declare a double variable amount with value 600
Output: Discounted Price: 540

Problem 10: Temperature Converter

Description: Write a Java program that categorizes a temperature value as β€œCold”, β€œModerate”, or β€œHot” based on its value using an if-else ladder.

Example:

Input: Declare a double variable temperature with value 35.0
Output: Hot