w3resource logo


Java Programming Exercies

Java Conditional Statement : Exercises, Practice, Solution

Secondary Nav

Java Conditional Statement Exercises [22 exercises with solution]

1. Write a Java program to get a number from the user and print whether it is positive or negative. Go to the editor

Test Data
Input number: 35
Expected Output :
Number is positive

Click me to see the solution

2. Write a Java program to solve quadratic equations (use if, else if and else). Go to the editor

Test Data
Input a: 1
Input b: 5
Input c: 1
Expected Output :
The roots are -0.20871215252208009 and -4.7912878474779195

Click me to see the solution

3. Take three numbers from the user and print the greatest number. Go to the editor

Test Data
Input the 1st number: 25
Input the 2nd number: 78
Input the 3rd number: 87
Expected Output :
The greatest: 87

Click me to see the solution

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. Go to the editor

Test Data
Input value: 25
Expected Output :
Positive number

Click me to see the solution

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. Go to the editor

Test Data
Input number: 3
Expected Output :
Wednesday

Click me to see the solution

6. Write a Java program that reads in two floating-point numbers and tests whether they are the same up to three decimal places. Go to the editor

Test Data
Input floating-point number: 1256
Input floating-point another number: 3254
Expected Output :
They are different

Click me to see the solution

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

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

Click me to see the solution

8. Write a Java program that takes the user to provide a single character from the alphabet. Print Vowel of Consonant, depending on the 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. Go to the editor

Test Data
Input an alphabet: p
Expected Output :
Input letter is Consonant

Java Code:

Click me to see the solution

9. Write a Java program that takes a year from user and print whether that year is a leap year or not. Go to the editor

Test Data
Input the year: 2016
Expected Output :
2016 is a leap year

Click me to see the solution

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

Expected Output :

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

Click me to see the solution

11. Write a program in Java to display n terms of natural numbers and their sum. Go to the editor

Test Data
Input the number: 2
Expected Output :

Input number:                                                                    
2                                                                                
The first n natural numbers are :                                                
2                                                                                
1                                                                                
2                                                                                
The Sum of Natural Number upto n terms :                                         
23

Click me to see the solution.

12. Write a program in Java to input 5 numbers from keyboard and find their sum and average. Go to the editor

Test Data
Input the 5 numbers : 1 2 3 4 5
Expected Output :

Input the 5 numbers :                                                            
1                                                                                
2                                                                                
3                                                                                
4                                                                                
5                                                                                
The sum of 5 no is : 15                                                          
The Average is : 3.0                          

Click me to see the solution

13. Write a program in Java to display the cube of the number upto given an integer. Go to the editor

Test Data
Input number of terms : 4
Expected Output :

Number is : 1 and cube of 1 is : 1                                               
Number is : 2 and cube of 2 is : 8                                               
Number is : 3 and cube of 3 is : 27                                              
Number is : 4 and cube of 4 is : 64

Click me to see the solution

14. Write a program in Java to display the multiplication table of a given integer. Go to the editor

Test Data
Input the number (Table to be calculated) : Input number of terms : 5
Expected Output :

5 X 0 = 0                                                                        
5 X 1 = 5                                                                        
5 X 2 = 10                                                                       
5 X 3 = 15                                                                       
5 X 4 = 20                                                                       
5 X 5 = 25

Click me to see the solution

15. Write a program in Java to display the n terms of odd natural number and their sum. Go to the editor

Test Data
Input number of terms is: 5
Expected Output :

The odd numbers are :                                                            
1                                                                                
3                                                                                
5                                                                                
7                                                                                
9                                                                                
The Sum of odd Natural Number upto 5 terms is: 25

Click me to see the solution

16. Write a program in Java to display the pattern like right angle triangle with a number. Go to the editor

Test Data
Input number of rows : 10
Expected Output :

1                                                                                
12                                                                               
123                                                                              
1234                                                                             
12345                                                                            
123456                                                                           
1234567                                                                          
12345678                                                                         
123456789                                                                        
12345678910

Click me to see the solution

17. Write a program in Java to make such a pattern like right angle triangle with a number which will repeat a number in a row.The pattern is as follows : Go to the editor

1
22
333
4444

Click me to see the solution

18. Write a program in Java to make such a pattern like right angle triangle with number increased by 1.The pattern like : Go to the editor

1
2 3
4 5 6
7 8 9 10 

Click me to see the solution

19. Write a program in Java to make such a pattern like a pyramid with a number which will repeat the number in the same row. Go to the editor

       1
      2 2
     3 3 3
    4 4 4 4 

Click me to see the solution

20. Write a program in Java to print the Floyd's Triangle. Go to the editor

Test Data
Input number of rows : 5
Expected Output :

1                                                                                
01                                                                               
101                                                                              
0101                                                                             
10101

Click me to see the solution

21. Write a program in Java to display the pattern like a diamond. Go to the editor

Test Data
Input number of rows (half of the diamond) : 7
Expected Output :

                                                                                 
      *                                                                          
     ***                                                                         
    *****                                                                        
   *******                                                                       
  *********                                                                      
 ***********                                                                     
*************                                                                    
 ***********                                                                     
  *********                                                                      
   *******                                                                       
    *****                                                                        
     ***                                                                         
      *                     
 

Click me to see the solution

22. Write a Java program to display Pascal's triangle. Go to the editor

Test Data
Input number of rows: 5
Expected Output :

Input number of rows: 5                                                          
      1                                                                          
     1 1                                                                         
    1 2 1                                                                        
   1 3 3 1                                                                       
  1 4 6 4 1                      
 

Click me to see the solution

Java Practice online





List of Exercises with Solutions :



Join our Question Answer community to learn and share your programming knowledge.