w3resource logo


Java Programming Exercises

Java Basic Programming : Exercises, Practice, Solution

Secondary Nav

Java Basic Exercises [39 exercises with solution]

1. Write a Java program to print 'Hello' on screen and then print your name on a separate line. Go to the editor
Expected Output :
Hello
Alexandra Abramov

Click me to see the solution

2. Write a Java program to print the sum of two numbers. Go to the editor
Test Data:
74 + 36
Expected Output :
110

Click me to see the solution

3. Write a Java program to divide two numbers and print on the screen. Go to the editor
Test Data :
50/3
Expected Output :
16

Click me to see the solution

4. Write a Java program to print the result of the following operations. Go to the editor
Test Data:
a. -5 + 8 * 6
b. (55+9) % 9
c. 20 + -3*5 / 8
d. 5 + 15 / 3 * 2 - 8 % 3
Expected Output :
43
1
19
13

Click me to see the solution

5. Write a Java program that takes two numbers as input and display the product of two numbers. Go to the editor
Test Data:
Input first number: 25
Input second number: 5
Expected Output :
25 x 5 = 125

Click me to see the solution

6. Write a Java program to print the sum (addition), multiply, subtract, divide and remainder of two numbers. Go to the editor
Test Data:
Input first number: 125
Input second number: 24
Expected Output :
125 + 24 = 149
125 - 24 = 101
125 x 24 = 3000
125 / 24 = 5
125 mod 24 = 5

Click me to see the solution

7. Write a Java program that takes a number as input and prints its multiplication table upto 10. Go to the editor
Test Data:
Input a number: 8
Expected Output :
8 x 1 = 8
8 x 2 = 16
8 x 3 = 24
...
8 x 10 = 80

Click me to see the solution

8. Write a Java program to display the following pattern. Go to the editor
Sample Pattern :

   J    a   v     v  a                                                  
   J   a a   v   v  a a                                                 
J  J  aaaaa   V V  aaaaa                                                
 JJ  a     a   V  a     a
 

Click me to see the solution

9. Write a Java program to compute the specified expressions and print the output. Go to the editor
Test Data:
((25.5 * 3.5 - 3.5 * 3.5) / (40.5 - 4.5))
Expected Output
2.138888888888889

Click me to see the solution

10. Write a Java program to compute a specified formula. Go to the editor
Specified Formula :
4.0 * (1 - (1.0/3) + (1.0/5) - (1.0/7) + (1.0/9) - (1.0/11))
Expected Output
2.9760461760461765

Click me to see the solution

11. Write a Java program to print the area and perimeter of a circle. Go to the editor
Test Data:
Radius = 7.5
Expected Output
Perimeter is = 47.12388980384689
Area is = 176.71458676442586

Click me to see the solution

12. Write a Java program that takes three numbers as input to calculate and print the average of the numbers. Go to the editor
Click me to see the solution

13. Write a Java program to print the area and perimeter of a rectangle. Go to the editor
Test Data:
Width = 5.5 Height = 8.5

Expected Output
Area is 5.6 * 8.5 = 47.60
Perimeter is 2 * (5.6 + 8.5) = 47.60

Click me to see the solution

14. Write a Java program to print an American flag on the screen. Go to the editor
Expected Output

* * * * * * ==================================                          
 * * * * *  ==================================                          
* * * * * * ==================================                          
 * * * * *  ==================================                          
* * * * * * ==================================                          
 * * * * *  ==================================                          
* * * * * * ==================================                          
 * * * * *  ==================================                          
* * * * * * ==================================                          
==============================================                          
==============================================                          
==============================================                          
==============================================                          
==============================================                          
==============================================
 

Click me to see the solution

15. Write a Java program to swap two variables. Go to the editor
Click me to see the solution

16. Write a Java program to print a face. Go to the editor
Expected Output

 +"""""+                                                 
[| o o |]                                                
 |  ^  |                                                 
 | '-' |                                                 
 +-----+

Click me to see the solution

17. Write a Java program to add two binary numbers. Go to the editor
Input Data:
Input first binary number: 10
Input second binary number: 11
Expected Output

Sum of two binary numbers: 101

Click me to see the solution

18. Write a Java program to multiply two binary numbers. Go to the editor
Input Data:
Input the first binary number: 10
Input the second binary number: 11
Expected Output

Product of two binary numbers: 110 

Click me to see the solution

19. Write a Java program to convert a decimal number to binary number. Go to the editor
Input Data:
Input a Decimal Number : 5
Expected Output

Binary number is: 101 

Click me to see the solution

20. Write a Java program to convert a decimal number to hexadecimal number. Go to the editor
Input Data:
Input a decimal number: 15
Expected Output

Hexadecimal number is : F 

Click me to see the solution

21. Write a Java program to convert a decimal number to octal number. Go to the editor
Input Data:
Input a Decimal Number: 15
Expected Output

Octal number is: 17  

Click me to see the solution

22. Write a Java program to convert a binary number to decimal number. Go to the editor
Input Data:
Input a binary number: 100
Expected Output

Decimal Number: 4 

Click me to see the solution

23. Write a Java program to convert a binary number to hexadecimal number. Go to the editor
Input Data:
Input a Binary Number: 1101
Expected Output

HexaDecimal value: D

Click me to see the solution

24. Write a Java program to convert a binary number to a Octal number. Go to the editor
Input Data:
Input a Binary Number: 111
Expected Output

Octal number: 7 

Click me to see the solution

25. Write a Java program to convert a octal number to a decimal number. Go to the editor
Input Data:
Input any octal number: 10
Expected Output

Equivalent decimal number: 8

Click me to see the solution

26. Write a Java program to convert a octal number to a binary number. Go to the editor
Input Data:
Input any octal number: 7
Expected Output

Equivalent binary number: 111 

Click me to see the solution

27. Write a Java program to convert a octal number to a hexadecimal number. Go to the editor
Input Data:
Input a octal number : 100
Expected Output

Equivalent hexadecimal number: 40

Click me to see the solution

28. Write a Java program to convert a hexadecimal to a decimal number. Go to the editor
Input Data:
Input a hexadecimal number: 25
Expected Output

Equivalent decimal number is: 37

Click me to see the solution

29. Write a Java program to convert a hexadecimal to a binary number. Go to the editor
Input Data:
Enter Hexadecimal Number : 37
Expected Output

Equivalent Binary Number is: 110111

Click me to see the solution

30. Write a Java program to convert a hexadecimal to a octal number. Go to the editor
Input Data:
Input a hexadecimal number: 40
Expected Output

Equivalent of octal number is: 100 

Click me to see the solution

31. Write a Java program to check whether Java is installed on your computer. Go to the editor
Expected Output

Java Version: 1.8.0_71                                                            
Java Runtime Version: 1.8.0_71-b15                                                
Java Home: /opt/jdk/jdk1.8.0_71/jre                                               
Java Vendor: Oracle Corporation                                                   
Java Vendor URL: http://java.oracle.com/                                          
Java Class Path: .

Click me to see the solution

32. Write a Java program to compare two numbers. Go to the editor
Input Data:
Input first integer: 25
Input second integer: 39
Expected Output

25 != 39                                                                          
25 < 39                                                                           
25 <= 39

Click me to see the solution

33. Write a Java program and compute the sum of the digits of an integer. Go to the editor
Input Data:
Input an integer: 25
Expected Output

The sum of the digits is: 7

Click me to see the solution

34. Write a Java program to compute the area of a hexagon. Go to the editor
Area of a hexagon = (6 * s^2)/(4*tan(π/6))
where s is the length of a side
Input Data:
Input the length of a side of the hexagon: 6
Expected Output

The area of the hexagon is: 93.53074360871938

Click me to see the solution

35. Write a Java program to compute the area of a polygon. Go to the editor
Area of a polygon = (n*s^2)/(4*tan(π/n))
where n is n-sided polygon and s is the length of a side
Input Data:
Input the number of sides on the polygon: 7
Input the length of one of the sides: 6
Expected Output

The area is: 130.82084798405722

Click me to see the solution

36. Write a Java program to compute the distance between two points on the surface of earth. Go to the editor
Distance between the two points [ (x1,y1) & (x2,y2)]
d = radius * arccos(sin(x1) * sin(x2) + cos(x1) * cos(x2) * cos(y1 - y2))
Radius of the earth r = 6371.01 Kilometers
Input Data:
Input the latitude of coordinate 1: 25
Input the longitude of coordinate 1: 35
Input the latitude of coordinate 2: 35.5
Input the longitude of coordinate 2: 25.5
Expected Output

The distance between those points is: 1480.0848451069087 km 

Click me to see the solution

37. Write a Java program to reverse a string. Go to the editor
Input Data:
Input a string: The quick brown fox
Expected Output

Reverse string: xof nworb kciuq ehT

Click me to see the solution

38. Write a Java program to count the letters, spaces, numbers and other characters of an input string. Go to the editor
Expected Output

letter: 23                                               
space: 9                                                 
number: 10                                               
other: 6

Click me to see the solution

39. Write a Java program to create and display unique three-digit number using 1, 2, 3, 4. Also count how many three-digit numbers are there. Go to the editor
Expected Output

123                                                      
124                                                      
...                                            
                                                   
431                                                      
432                                                      
Total number of the three-digit-number is 24

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.

Solve these problems:

Java: How to convert a string to an integer in Java?

C#: Loops in c#

SQL: JOIN using more than 5 tables