Programming with JAVA
Sample Programs of conditional
Statements
1. WAP to check if the entered number is even or odd using Scanner class.
2. WAP to check the largest among two numbers entered by user using wrapper
class (Command line).
3. WAP to get a number from the user and print whether it is positive or
negative.
4. WAP to get one number from user and check if the entered number is
divisible by 5 or not
5. WAP to get one number from user and check if the entered number is
divisible by 5 and 11 or not
6. WAP to get one number from user and check if the entered number is
divisible by 5 but not by 11.
7. WAP in which a user enters a number, and the program will find the Even and
odd number. Your program should only accept integers of length 2 or higher.
8. WAP to find all the roots of a quadratic equation. Using if-else.
9. WAP to check whether a year is a leap year or not by using an if-else
statement. Leap year program
10. WAP to print absolute value of a number entered by user. E.g.-
INPUT: 1 OUTPUT: 1
INPUT: -1 OUTPUT: 1
11. WAP to take three numbers from the user and print the greatest number.
12. WAP to take input of age of 3 people by user and determine oldest and
youngest among them.
13. WAP 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.
14. WAP that takes the user to provide a single character from the alphabet. Print
Vowel or Consonant, depending on the user input.
15. WAP that reads in two floating-point numbers and tests whether they are the
same up to three decimal places.
16. WAP to take a value from the user as input any character and check whether
it is the alphabet, digit or special character. Using if-else.
17. WAP to check whether a character is an uppercase or lowercase alphabet.
Using if-else.
18. WAP to take value from the user as an input weekday number and print
weekday by using if-else statement. Weekday program using if else and
switch statement both separately.
19. WAP to take a value from the user as input the month number and print
number of days in that month. Using if-else.
Page 1 of 3
Programming with JAVA
20. WAP to take the hours and minutes as input by the user and the show that
whether it is AM or PM using if-else statement
21. WAP to swap the values of two number if values of both variables are not the
same using if-else statement
22. WAP for a school has following rules for grading system:
a. Below 25 - F
b. 25 to 45 - E
c. 45 to 50 - D
d. 50 to 60 - C
e. 60 to 80 - B
f. Above 80 - A
Ask user to enter marks and print the corresponding grade.
23. If
x= 2
y= 5
z=0
then find values of the following expressions:
a. x == 2
b. x != 5
c. x != 5 && y >= 5
d. z != 0 || x == 2
e. !(y < 10)
24. WAP to take a value from the user as input the basic salary of an employee
and calculate its Gross salary according to the following:
Basic Salary <= 10000: HRA = 20%, DA = 80%
Basic Salary <= 20000 : HRA = 25%, DA = 90%
Basic Salary > 20000 : HRA = 30%, DA = 95%.
Where HRA and DA is applicable on Basic salary and being add ones on total
salary.
25. WAP to take a value from the user as input electricity unit charges and
calculate total electricity bill according to the given condition:
For the first 50 units Rs. 0.50/unit
For the next 100 units Rs. 0.75/unit
For the next 100 units Rs. 1.20/unit
For unit above 250 Rs. 1.50/unit
An additional surcharge of 20% is added to the bill. Display the total payable
bill amount using if else.
26. WAP to count the total number of notes in a given amount. Using if-else
27. WAP to take a value from the user as input all sides of a triangle and check
whether the triangle is valid or not. Using if-else. If the triangle is valid then
check whether the triangle is an equilateral, isosceles or scalene triangle.
28. WAP to take a value from the to take a value from the user as input marks of
five subjects Physics, Chemistry, Biology, Mathematics, and Computer.
Calculate percentage and grade according to the following:
Percentage >= 90% : Grade A,
Percentage >= 80% : Grade B,
Page 2 of 3
Programming with JAVA
Percentage >= 70% : Grade C,
Percentage >= 60% : Grade D,
Percentage >= 40% : Grade E,
Percentage < 40% : Grade F,
Write this program with the help of switch statement. Grade and Percentage
Program
29. WAP for a shop. A shop will give discount of 10% if the cost of purchased
quantity is more than 1000. Ask user for appropriate quantity. Display the total
payable amount for a customer. Now if the customer has bought this book at
discounted price than he is selling them in a market to person A. Take the
selling price from the user and calculate the total profit/loss of this deal.
30. Write a JAVA program to create the following game using appropriate control
statements, input statements and print statements:
Let the player choose a door from door 1 and door 2
If player selected door 1, it contains a giant bear eating a cheese cake… Give
the following options to the player -to take the cake, to scream at the bear or
do anything else.
If the player takes the cake, the bear eats his face off. If the player screams at
the bear, the bear eats his legs off. If the player does anything else, the bear
runs away.
If player selected door 2, he sees the eyes of a huge monster.
Ask the player what he sees in the eyes of the huge monster: blueberries,
muffins or revolvers.
If it is muffins, the player survives otherwise, the user becomes insane.
In the beginning of the game, if the player selects any door other than door 1
or door 2, he stumbles around and falls on a knife and dies.
In the end, discuss what kinds of inputs from the player would result in the
player winning the game.
Page 3 of 3