Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Conditionals’ in java

1. Write a Java program to check if a number is positive, negative, or zero.

2. Create a program that determines if a number is even or odd.

3. Write a Java program to find the maximum of two numbers using if-else statements.

4. Implement a program that calculates the average of three numbers.

5. Develop a program to check if a year is a leap year.

6. Write a program that determines the largest of three numbers.

7. Create a program to check if a character is a vowel or a consonant.

8. Write a program to find the largest among four numbers.

9. Create a program that checks if a number is a prime number.

10. Implement a simple calculator program that performs addition, subtraction, multiplication, or
division based on user input.

11. Write a program to determine the day of the week based on user input (e.g., 1 for Sunday, 2 for
Monday, etc.).

12. Create a program to calculate the area of a rectangle or a triangle based on user input.

13. Write a program to determine the season (spring, summer, fall, or winter) based on the month
provided by the user.

14. Implement a program to convert temperature from Celsius to Fahrenheit or vice versa based on
user choice.

15. Create a program to check if a user-inputted year is a century year (ending in 00).

16. Write a program to determine the eligibility for voting based on the age provided by the user.

17. Implement a simple grading system that converts a numerical score to a letter grade (A, B, C, D,
or F).

18. Write a program to find the roots of a quadratic equation and display the solutions.

19. Create a program that checks if a user-inputted number is a perfect number or not.

20. Implement a menu-driven program that allows users to perform various math operations (e.g.,
addition, subtraction, multiplication) using switch statements.

These exercises cover a wide range of conditional statements and can help you improve your Java
programming skills. Try solving them to enhance your understanding of conditionals in Java.
Switch in java
1. Write a program that takes a day of the week (as a number) and prints the corresponding day
name using a `switch` statement.

2. Create a program that reads a month (as a number) and outputs the number of days in that
month using a `switch` statement.

3. Build a simple calculator program that performs addition, subtraction, multiplication, and division
based on user input with a `switch` statement.

4. Write a program that takes a grade (A, B, C, D, or F) and prints a corresponding message using a
`switch` statement.

5. Create a program that takes an integer and determines whether it's even or odd using a `switch`
statement.

6. Write a program that converts a numeric grade (e.g., 92) to a letter grade (A, B, C, D, F) using a
`switch` statement.

7. Implement a program that takes a season (spring, summer, autumn, or winter) and prints a
message indicating which months are in that season using a `switch` statement.

8. Develop a program that takes a single character and determines if it is a vowel or a consonant
using a `switch` statement.

9. Create a simple menu-driven program that allows the user to select options (e.g., display a
message, quit) using a `switch` statement.

10. Write a program that takes a number from 1 to 12 and displays the corresponding month name
using a `switch` statement.

11. Implement a program that calculates the area or perimeter of a square or rectangle based on
user input using a `switch` statement.

12. Build a program that takes a country code (e.g., USA, UK, France) and prints the corresponding
currency using a `switch` statement.

13. Write a program that determines whether a given year is a leap year or not using a `switch`
statement.

14. Create a program that takes a day, month, and year as input and prints the date in a specific
format (e.g., "MM/DD/YYYY") using a `switch` statement.

15. Build a program that converts between different temperature units (e.g., Celsius to Fahrenheit,
Fahrenheit to Celsius) based on user input with a `switch` statement.

16. Implement a program that simulates a simple traffic light with three states (red, yellow, green)
using a `switch` statement.
17. Write a program that takes a number and determines if it's positive, negative, or zero using a
`switch` statement.

18. Create a program that takes a simple arithmetic expression (e.g., 2 + 3) and evaluates it using a
`switch` statement.

19. Build a program that reads a fruit name and prints its color using a `switch` statement.

20. Implement a program that simulates a basic ATM with options like checking balance, deposit,
and withdraw using a `switch` statement.

These exercises should help you practice using the `switch` statement in Java and improve your
problem-solving skills.

Loops in java
1. Write a program to print the numbers from 1 to 10 using a `for` loop.

2. Create a program to print even numbers from 2 to 20 using a `for` loop.

3. Write a Java program to calculate the factorial of a number using a `for` loop.

4. Write a program that prints the Fibonacci sequence up to the nth term using a `for` loop.

5. Create a program to find the sum of all the numbers from 1 to 100 using a `while` loop.

6. Write a program to print a multiplication table for a given number using a `for` loop.

7. Create a program to find the largest number in an array of integers using a `for` loop.

8. Write a Java program to reverse a string using a `while` loop.

9. Create a program that checks if a number is prime using a `for` loop.

10. Write a program to find the common elements between two arrays using nested `for` loops.

11. Create a program to print the first 10 natural numbers in reverse order using a `for` loop.

12. Write a program to calculate the sum of digits in a number using a `while` loop.

13. Create a program that prints a pattern of stars in a right-angled triangle using nested `for` loops.

14. Write a Java program to calculate the average of an array of numbers using a `for` loop.

15. Create a program that finds the factorial of a number using a `do-while` loop.

16. Write a program to check if a given string is a palindrome using a `while` loop.

17. Create a program that prints all prime numbers between 1 and 100 using a `for` loop.

18. Write a Java program to find the GCD (Greatest Common Divisor) of two numbers using a `while`
loop.
19. Create a program to calculate the power of a number using a `for` loop (e.g., 2^5).

20. Write a program to find the factorial of a number using recursion.

These exercises cover a range of loop-related concepts in Java and should help you improve your
skills with loops in Java.

You might also like