Simple Java Programs for Beginners | Java Programming Examples

1. Write a Java program to print “Hello JAVA”

Solution:

Output:

Hello JAVA

2. Write a java program to print the following:

Hello

Welcome to the world of JAVA

Let us learn JAVA

Solution:

Output:

Hello

Welcome to the world of JAVA

Let us learn JAVA

3. Write a Java program to print the sum of two numbers.

Solution:

Output:

The sum is: 30

4. Write a program to calculate the distance that light travels in 1000 days using long variables. Approximate speed of light in miles per second is 86000.

Solution:

Output:

In 1000 days, light will travel about 7430400000000 miles

5. Write a Java program to multiply two Floating-Point numbers.

Solution:

Output:

The product is: 10.349999

The product is: 10.35

6. Write a program to calculate the area of a circle using double.

Solution:

Output:

Area of circle is:346.4055

7. Write a program to use char variable.

Solution:

Output:

ch1 and ch2:X Y

Now ch1 is: Y

8. Write a program to use Boolean values.

Solution:

Output:

b is: false

b is: true

9. Write a program to add two numbers taking input from user.

Solution:

Output:

Enter 2 numbers: 12 34

The sum is: 46

10. Write a Java program that takes two numbers as input and displays the product of two numbers.

Solution:

Output:

Enter 2 numbers: 5 8

The product is: 40

11. Write a Java program to divide two numbers and print them on the screen.

Solution:

Output:

Enter 2 numbers: 40 8

The division value is: 5

12. Write a Java program to print the sum, multiply, subtract, divide and remainder of two numbers.

Solution:

Output:

Enter 2 numbers: 25 5

25 + 5 = 30

25 + 5 = 20

25 + 5 = 125

25 + 5 = 5

25 + 5 = 0

13. Write a java program to find the square of a number.

Solution:

Output:

Square of 5 is: 25

OR

Output:

Enter a number: 8

Square of 8 is: 64

14. Write a java program to find the square of a number using pow() function.

Solution:

Output:

Enter a number: 12

Square of 12.0 is: 144.0

15. Write a java program to find the square root of a number.

Solution:

Output:

Enter a number: 256

Square root of 256.0 is: 16.0

16. Write a Java Program to Calculate Simple Interest.

Solution:

Output:

Simple Interest = 400.0

17. Write a program to calculate simple interest taking input form user.

    Solution:

    Output:

    Enter principal amount: 40000

    Enter the rate: 6

    Enter the time: 5

    The simple interest is: 12000.0

    18. Write a Java program to print the area and perimeter of a circle. Given radius=6.7 (Area of Circle=πr2, Perimeter of a circle=2πr)

    Solution:

    Output:

    Area of the circle: 141.04438000000002

    Area of the circle: 141.044

    Perimeter of the circle: 42.1028

    Perimeter of the circle: 42.10

    19. Write a Java program to find the area and perimeter of a circle whose radius is user input.

    Solution:

    Output:

    Enter the radius of the circle: 6.7
    Area of the circle: 141.02609421964584
    Perimeter of the circle: 42.09734155810323

    20. Write a Java program to find area and perimeter of a square.

    Solution:

    Output:

    Enter the length of side of square: 10

    Area of the square: 100.0

    Perimeter of the square:40.0

    21. Write a JAVA program to convert temperature from Fahrenheit to Celsius degree.

    Solution:

    Output:

    Enter temperature in Fahrenheit: 208.4
    Temperature in Celsius: 98.0

    22. Write a Java program to calculate total and average marks of three subjects of a student.

    Solution:

    Output:

    Enter marks of 3 subjects: 50 40 60

    Total Marks: 150

    Average Marks: 50.0

    23. Write a Java program to swap two numbers.

    Solution:

    Output:

    Enter two numbers: 10 20

    Before swapping:

    a= 10  b= 20

    After swapping:

    a= 20  b= 10

    24. Write a Java program to swap two numbers without using third variable.

      Solution:

      Output:

      Enter two numbers: 10 20

      Before swapping:

      a= 10  b= 20

      After swapping:

      a= 20  b= 10

      25. The basic salary of an employee is input through the keyboard. The DA is 15% of the basic salary while the HRA is 25% of the basic salary. Write a program to calculate his total salary.

      Solution:

      Output:

      Enter basic salary: 40000

      Basic Salary: 40000.0

      Dearness Allowance: 6000.0

      House Rent Allowance: 10000.0

      Total Salary: 56000.0

      26.Write a Java program to check a number is even or odd.

      Solution:

      Output-1:

      Enter a number: 30

      30 is Even Number

      Output-2:

      Enter a number: 55

      55 is Odd Number

      27. Write a Java program to find a year is a Leap Year or not

      Solution:

      Output-1:

      Enter a year: 2020

      2020 is a leap year

      Output-2:

      Enter a year: 2025

      2025 is not a leap year

      28. Write a JAVA program to take three numbers from the user and print the largest number.

      Solution:

      Output:

      Enter 3 numbers: 34 78 12

      78 is the largest number

      29. Write a Java program to find the largest among three numbers using conditional operator.

      Solution:

      Output:

      Enter 3 numbers: 45 67 12

      Largest no is=67

      30. Write Java program to check a character is vowel or not.

      Solution:

      OR

      Output:

      A is a vowel

      31. Write Java program to check a character is vowel or not taking input from user.

      Solution:

      Output-1:

      Enter a character: U

      U is a vowel

      Output-2:

      Enter a character: R

      R is not a vowel

      32. Write a Java program to print first 100 natural numbers.

                              OR

      Write a program in Java to print the number from 1 to 100.

      Solution:

      Output:

      The first 100 natural numbers:

      1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

      33. Write a program in Java to display series of number from 10 to 60.

      Solution:

      Output:

      Numbers from 10 to 60:

      10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60


      34. Write a java program to calculate sum of first n natural numbers. 

      Solution:

      Output:

      Enter number to find the sum: 10

      Sum of first 10 natural numbers = 55

      35. Write a Java program to print the following pattern:

      *

      * *

      * * *

      * * * *

      * * * * *

      Solution:

      Output:

      *

       * *

       * * *

       * * * *

       * * * * *

      36. Write a Java program to print the following pattern:

      1

      2 2

      3 3 3

      4 4 4 4

      5 5 5 5 5

      6 6 6 6 6 6

      Solution:

      Output:

      1

       2 2

       3 3 3

       4 4 4 4

       5 5 5 5 5

       6 6 6 6 6 6

      37. Write a Java program to display a specific multiplication table.

      Solution:

      Output:

      Enter a number: 5
      Multiplication table of 5
      5×1=5
      5×2=10
      5×3=15
      5×4=20
      5×5=25
      5×6=30
      5×7=35
      5×8=40
      5×9=45
      5×10=50

      38. Write a Java program to print multiplication table from 1 to 5.

      Solution:

      Output:

      Multiplication table from 1 to 5:

      1×1=1

      1×2=2

      1×3=3

      1×4=4

      1×5=5

      1×6=6

      1×7=7

      1×8=8

      1×9=9

      1×10=10

      ……………

      …………….

      ……………

      3 thoughts on “Simple Java Programs for Beginners | Java Programming Examples”

      Leave a Comment