13 Basic Java Programs for beginner


In this post, you will learn different types of explained basic programs of JAVA for the Beginner level. Try all programs to clear the basic concept of the JAVA Program.


1. Program to compute Addition.

import java.util.Scanner;

 

public class Addition {

 

       public static void main(String[] args) {

             // TODO Auto-generated method stub

             Scanner sc = new Scanner(System.in);

             int number1, number2, addtion;

             System.out.println("Please enter value of number1");

             number1 = sc.nextInt(); // user input for number1

            

             System.out.println("Please enter value of number2");

             number2 = sc.nextInt(); // user input for number2

            

             addtion = number1 + number2; // addition

             System.out.println("The sum of "+number1 + " and " +number2 + " is " + addtion);

       }

}


Output:

Please enter value of number1

100

Please enter value of number2

150

The sum of 100 and 150 is 250

 

2. . Program on Area of circle and Rectangle

import java.util.Scanner;

 

public class AreaCalc {

       public static void main(String[] args) {

             double radius, areac;

             int length, breadth, arear;

             Scanner sc = new Scanner(System.in);

             System.out.println("-----CIRCLE-----");

             System.out.println("Enter radius of circle : ");

             radius = sc.nextDouble();

             areac = 3.14 * radius * radius;

             System.out.println("Area of Circle is : " + areac);

             System.out.println("-----RECTANGLE-----");

             System.out.println("Enter length and breadth of rectangle : ");

             length = sc.nextInt();

             breadth = sc.nextInt();

             arear = length * breadth;

             System.out.println("Area of Rectangle is : " + arear);

             sc.close();

       }

}

 


Output:

-----CIRCLE-----

Enter radius of circle :

4

Area of Circle is : 50.24

-----RECTANGLE-----

Enter length and breadth of rectangle :

 


3. Program on Mod,Div Operator to check the number of notes in ATM Transaction

import java.util.Scanner;

public class ATMNotes {

       public static void main(String[] args) {

             // TODO Auto-generated method stub

             Scanner sc = new Scanner(System.in);

             int amount;

             int notes;

             System.out.println("Enter amount : ");

             amount = sc.nextInt();

             if (amount % 100 != 0) {

                    System.out.println("Invalid amount entered");

             } else {

                    if (amount >= 1000) {

                           notes = amount / 1000;

                           amount = amount % 1000;

                           System.out.println("notes of 1000 : " + notes);

                    }

                    if (amount >= 500) {

                           notes = amount / 500;

                           amount = amount % 500;

                           System.out.println("notes of 500 : " + notes);

                    }

                    if (amount >= 100) {

                           notes = amount / 100;

                           amount = amount % 100;

                           System.out.println("notes of 100 : " + notes);

                    }

             }

       }

}


Output1:

Enter amount :

250

Invalid amount entered

 

Output2:

Enter amount :

1600

notes of 1000 : 1

notes of 500 : 1

notes of 100 : 1

 

4. Program to perform an arithmetic operation using switch case

import java.util.Scanner;

public class ArithmeticSwitch {

 

       public static void main(String[] args) {

             // TODO Auto-generated method stub

             Scanner sc = new Scanner(System.in);

             int choice, a = 0, b = 0;

             int add, sub, mult;

             double div;

             do {

                    System.out.println("1.Add\n2.Substract\n3.Multiply\n4.devide\n5.exit");

                    System.out.println("\nEnter yout choice");

                    choice = sc.nextInt();

                    System.out.println("enter value of a: ");

                    a = sc.nextInt();

                    System.out.println("enter value of b: ");

                    b = sc.nextInt();

                    switch (choice) {

                    case 1:

                           System.out.println("-----Addition-----");

                           add = a + b;

                           System.out.println("Addition is " + add);

                           break;

 

                    case 2:

                           System.out.println("-----Subtraction-----");

                           sub = a - b;

                           System.out.println("Subtraction is " + sub);

                           break;

                    case 3:

                           System.out.println("-----Multiplication-----");

                           mult = a * b;

                           System.out.println("Multiplication is " + mult);

                           break;

                    case 4:

                           System.out.println("-----Division-----");

                           div = (double) a / b;

                           System.out.println("division is " + div);

                           break;

                    case 5:

                           System.out.println("Thank you...");

                           System.exit(0);

                           break;

                    default:

                           System.out.println("U have entered wrong choice");

                           break;

                    }

             } while (choice != 5);

             sc.close();

       }

}
















































Output:

1.Add

2.Substract

3.Multiply

4.devide

5.exit

 

Enter yout choice

1

enter value of a:

10

enter value of b:

25

-----Addition-----

Addition is 35

1.Add

2.Substract

3.Multiply

4.devide

5.exit

 

Enter yout choice

2

enter value of a:

30

enter value of b:

5

-----Subtraction-----

Subtraction is 25

1.Add

2.Substract

3.Multiply

4.devide

5.exit

 

Enter yout choice

3

enter value of a:

15

enter value of b:

6

-----Multiplication-----

Multiplication is 90

1.Add

2.Substract

3.Multiply

4.devide

5.exit

 

Enter yout choice

4

enter value of a:

200

enter value of b:

16

-----Division-----

division is 12.5

1.Add

2.Substract

3.Multiply

4.devide

5.exit

 

Enter yout choice

5

Thank you...





























































5. Program to validate date,month and year using switch case. 

import java.util.Scanner;

public class DateValidationSwitch {

 

       public static void main(String[] args) {

             // TODO Auto-generated method stub

             Scanner sc = new Scanner(System.in);

             int dd, mm, yyyy;

             int no_of_days = 0;

             System.out.println("Enter date(dd): ");

             dd = sc.nextInt();

             System.out.println("Enter month(mm): ");

             mm = sc.nextInt();

             System.out.println("Enter year(yyyy): ");

             yyyy = sc.nextInt();

             switch (mm) {

             case 1:

             case 3:

             case 5:

             case 7:

             case 8:

             case 10:

             case 12:

                    no_of_days = 31;

                    break;

             case 4:

             case 6:

             case 9:

             case 11:

                    no_of_days = 30;

                    break;

             case 2:

                    if (yyyy % 4 == 0) {

                           no_of_days = 29;

                           System.out.println("This is leap year");

                    } else {

                           no_of_days = 28;

                    }

                    break;

             default:

                    System.out.println("You have entered invalid month");

             }// end switch

             if (dd <= no_of_days) {

                    System.out.println("Given date is valid");

             } else {

                    System.out.println("Given date is invalid");

             }

             sc.close();

       }

}

 

Output:

Enter date(dd):

29

Enter month(mm):

02

Enter year(yyyy):

2016

This is leap year

Given date is valid


6. Program using increment , decrement and ternary operators 

public class IncrementDecrement {

      

       public static void main(String[] args)

       {

       int a=10,b=20,c,d,e,g,h,i;

       System.out.println("values before increment "+a+" and "+b);

       c = ++a; // pre increment

       d = ++b;

       System.out.println("values after increment "+c+" and "+d);

       e= a>b ?++a:++b;

       System.out.println("turnary operator value "+e);

       g=e++; // post increment

       System.out.println("value after increment "+g);

       h=g--;

       System.out.println("value after decrement "+h);

       i=--h;

       System.out.println("value after decrement "+i);

       }

}

Output: 

values before increment 10 and 20

values after increment 11 and 21

turnary operator value 22

value after increment 22

value after decrement 22

value after decrement 21



7.  Program to calculate percentage of marks and display the grade.

import java.util.Scanner;

public class GradeCalc {

       public static void main(String[] args) {

             Scanner sc = new Scanner(System.in);

             double percentage, totalmarks;

             int math, eng, phys;

             System.out.println("enter your math score =");

             math = sc.nextInt();

             System.out.println("*************\nenter your eng score =");

             eng = sc.nextInt();

             System.out.println("*************\nenter your phys score =");

             phys = sc.nextInt();

             totalmarks = math + eng + phys;

             percentage = (double) (totalmarks / 300.0) * 100;

             System.out.println("your percentage : " + percentage);

             if (percentage >= 70) {

                    System.out.println("Congrats... you got First class with Distinction");

             } else if (percentage < 70 && percentage >= 60) {

                    System.out.println("Congrats... you got First class");

             } else if (percentage < 60 && percentage >= 40) {

                    System.out.println("Congrats... you are passed");

             } else {

                    System.out.println("Sorry... You are fail");

             }

             sc.close();

       }

 

}

 


Output:

enter your math score =

75

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

enter your eng score =

80

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

enter your phys score =

70

your percentage : 75.0

Congrats... you got First class with Distinction

 

8. Program to determine Prime Number. 

import java.util.Scanner;

public class PrimeNumber {

       public static void main(String[] args) {

             Scanner sc = new Scanner(System.in);

             int i, no;

             System.out.println("enter the no");

             no = sc.nextInt();

             for (i = 2; i <= no; i++) {

                    if (no % i == 0)

                           break;

             }

             if (i == no) {

                    System.out.println("Given number is a prime no");

             } else {

                    System.out.println("Given number is not a prime no");

             }

             sc.close();

       }

}

 


Output:

enter the no

79

Given number is a prime no



9. Program on Fibonacci series

import java.util.Scanner;

public class FibonnacciSeries {

       public static void main(String[] args) {

             Scanner sc = new Scanner(System.in);

             System.out.println("Enter the number for series : ");

             int num = sc.nextInt();

             int p = 0, q = 1, r = 0, i;

             System.out.println("The fibonnacci series for number " + num + " is:");

             for (i = 1; i < num; i++) {

                    System.out.print(p + " ");

                    r = p + q;

                    p = q;

                    q = r;

             }

       }

}


Output:

Enter the number for series :

5

The fibonnacci series for number 5 is:

0 1 1 2


10. Program on Reverse Number

import java.util.Scanner;

public class ReverseNumber {

       public static void main(String args[]) {

             int original, reverse = 0, number;

             Scanner in = new Scanner(System.in);

             System.out.println("Enter a number ");

             number = in.nextInt();

 

             original = number;

             while (number != 0) {

                    reverse = reverse * 10;

                    reverse = reverse + number % 10;

                    number = number / 10;

             }

             System.out.println("Reverse number : " + reverse);

       }

}

 


Output:

Enter a number

456

Reverse number : 654

 


11. Program to check number as Palindrome

/* A number is palindrome if the number and its reverse is same*/


import java.util.Scanner;

 

public class PalindromeNumber {

       public static void main(String args[]) {

             int original, reverse = 0, number;

             Scanner in = new Scanner(System.in);

             System.out.println("Enter a number to check if it is a palindrome");

             number = in.nextInt();

 

             original = number;

             while (number != 0) {

                    reverse = reverse * 10;

                    reverse = reverse + number % 10;

                    number = number / 10;

             }

             System.out.println("Reverse number : " + reverse);

 

             if (original == reverse) {

                    System.out.println("Numebr is palindrome");

             }

       }

}


Output:

Enter a number to check if it is a palindrome

121

Reverse number : 121

Numebr is palindrome

 


12. Program on Armstrong Number

     /* Armstrong number is an integer such that the sum of the cubes of its digits is equal to the number itself.*/

import java.util.Scanner;

public class ArmstrongNumber {

       public static void main(String arg[]) {

             Scanner sc = new Scanner(System.in);

             int no, rem = 0, x = 0;

             System.out.println("enter the number to check for armstrong");

             no = sc.nextInt();

             int p = no;

             while (no > 0) {

                    rem = no % 10;

                    x = x + (rem * rem * rem);

                    no = no / 10;

             }

             if (p == x) {

                    System.out.println("this is an armstrong");

             } else {

                    System.out.println("This is not an armstrong number");

             }

             sc.close();

       }

}

 


Output:

enter the number to check for armstrong

371

this is an armstrong

 


13. Programs on patterns:

public class Pattern1 {

       public static void main(String[] args) {

             for (int i = 1; i <= 5; i++) {

                    for (int j = 1; j <= 5; j++) {

                           System.out.print(i);

                    }

                    System.out.println("");

             }

       }   

}


Output:

11111

22222

33333

44444

55555


2. Pattern Sample 

12345
12345 
12345 
12345 
12345

public class ForSeries1 {

 

       public static void main(String[] args) {

             for (int i = 1; i <= 5; i++) {

                    for (int j = 1; j <= 5; j++) {

                           System.out.print(j);

                    }

                    System.out.println(" ");

             }

       }

}

 


Output:

12345

12345

12345

12345

12345

 


3. 
22 
333 
4444 
55555 

public class Pattern2 {

       public static void main(String args[]) {

             for (int i = 1; i <= 5; i++) {

                    for (int j = 1; j <= i; j++) {

                           System.out.print(i);

                    }

                    System.out.println("");

             }

       }

}


Output:

1

22

333

4444

55555


4. 
           
           * *
         * * *
        * * * *
      * * * * *
       * * * *
         * * *
          * *
           *

public class Forseries3 {

       public static void main(String[] args) {

             int i, j, k;

             {

                    for (i = 0; i < 5; i++) {

                           for (k = 5; k > i; k--) {

                                 System.out.print(" ");

                           }

                           for (j = 0; j < i; j++) {

                                 System.out.print("*");

                                 System.out.print(" ");

                           }

                           System.out.println("");

                    }

                    for (i = 0; i < 5; i++) {

                           for (k = 0; k < i; k++) {

                                 System.out.print(" ");

                           }

                           for (j = 5; j > i; j--) {

                                 System.out.print("*");

                                 System.out.print(" ");

                           }

                           System.out.println("");

                    }

             }

       }

}

 


Output:


    *

   * *

  * * *

 * * * *

* * * * *

 * * * *

  * * *

   * *

    *


5. 

         1 2 1 

     1 2 3 2 1 

    1 2 3 4 3 2 1

 1 2 3 4 5 4 3 2 1

1 2 3 4 5 6 5 4 3 2 1

public class Forloopdemo {

 

       public static void main(String[] args) {

             int i, j, k = 0;

             for (i = 1; i <= 5; i++) {

                    for (int m = 5; m >= i; m--) {

                           System.out.print(" ");

                    }

                    for (j = 1; j <= i; j++) {

                           System.out.print(j);

                           System.out.print(" ");

                    }

                    for (k = j; k > 0; k--) {

                           System.out.print(k);

                           System.out.print(" ");

                    }

                    System.out.println();

             }

       }

}

 


Output:

     1 2 1

    1 2 3 2 1

   1 2 3 4 3 2 1

  1 2 3 4 5 4 3 2 1

 1 2 3 4 5 6 5 4 3 2 1

 



6. 

    5
       4
          3 
             2
                 1

public class For_demo {

       public static void main(String[] args) {

             int k = 5;

             for (int i = 0; i < 5; i++) {

                    for (int m = 0; m < i; m++) {

                           System.out.print(" ");

                    }

                    for (int j = 0; j < 5; j++) {

                           if (i == j) {

                                 System.out.print(k);

                                 k--;

                           }

                    }

                    System.out.println("");

             }

       }

}


Output:

5

 4

  3

   2

    1


Above all the basic programs explained with all type of concept, by doing these programs you can know the basic concept of the JAVA programming. For more basic programs in Java  Click Here.


More Topics:

Core Java

Oops Features

String Handling


Previous
Next Post »