This page contains a ton of Examples that are purely concentrated on some java Basics concept

Java Beginners Example hide

1.Java program to find the ASCII value of the character

package javaBasics.examplesBeginners;

public class a
{
    public static void main(String[] args)
    {
        char c='A';
        int asciiCode=c;
        int asciiValue=(int)c;
        System.out.println("The ASCII value of 'A' is"+" "+asciiValue);


    }
}
Output
The ASCII value of 'A' is 65

2.Program to read two integers and print product of them

package javaBasics.javaBasicsExamples;

public class b
{
    public static void main(String[] args)
    {
        int a=10;
        int b=20;
        int c;
        c=a*b;
        System.out.println("The product of 2 numbers is"+" "+c);
    }
}
Output
The product of 2 numbers is 200

3.Java program to calculate the area of Triangle

package javaBasics.javaBasicsExamples;

public class c
{
    public static void main(String[] args) {
        double area;
        double base=10;
        double height=20;
        area=(base*height)/2;
        System.out.println("The area of Triangle is"+" "+area);
    }
}
Output
The area of Triangle is 100.0


4.Java Program to calculate and display Student Grades

Note:- ***Easy Don’t get fear by seeing the length of program***
Use of Scanner class{to take input from user}

***** Please press interactive mode ***** ***** { Circle should be on right side}*****

//Java Program to calculate and display Student Grades
package javaBasics.javaBasicsExamples;

import java.util.Scanner;

public class d
{
    public static void main(String[] args)
    {
//      Declare the Variables
//      Create an array to store the marks
        int marks[]=new int[6];

        int i;
        float  avg,total=0;

//      Scanner class to take input from user
        Scanner inputScan = new Scanner(System.in);

//      for loop to perform iterations
        for(i=0;i<6;i++)
        {
//       (i+1) it is used to change subject numbers like this till subject6
            /*subject1:
             *subject2:
             *subject3:
             *subject4:
             *subject5:
             *subject6:*/
            System.out.println("Enter the subject"+""+(i+1)+':');
            marks[i]=inputScan.nextInt();
            total=total+marks[i];
        }
        inputScan.close();

//      printing the total marks
        System.out.println("The total marks is"+" "+total);

//      Taking of avg of 6 subjects
        avg=total/6;
        System.out.println("The avg of 6 subject is"+" "+avg);

        System.out.println("The Grade of student is:");

//      ALLOTING THE GRADE
            if(avg>80)
            {
                System.out.println("A+");
            }
            else if(avg>=70 && avg<80)
            {
                System.out.println("A");
            }
            else if(avg>=60 && avg<70)
            {
                System.out.println("B");
            }
            else if(avg>=40 && avg<60)
            {
                System.out.println("C");
            }
            else
                {
                System.out.println("D");
            }
    }
}
Output
Enter the subject1:
70
Enter the subject2:
80
Enter the subject3:
90
Enter the subject4:
70
Enter the subject5:
60
Enter the subject6:
80
The total marks is 450.0
The avg of 6 subject is 75.0
The Grade of student is:
A

5.Java Program to check the number is Even or Odd

Use of Scanner class {to take input from user}

***** Please press interactive mode ***** ***** { Circle should be on right side}*****

package javaBasics.javaBasicsExamples;

import java.util.Scanner;

public class e
{
    public static void main(String[] args)
    {
        Scanner num1=new Scanner(System.in);
        System.out.println("Enter the number");
        int number=num1.nextInt();
        if (number % 2==0)
        {
            System.out.println("The number is even number");
        }
        else{
            System.out.println("The number is odd number");
        }
    }
}
Output
Enter the number
25
The number is odd number
Enter the number
10
The number is even number

6.Java Program to Check the given year is Leap Year or not

Use of Scanner class {to take input from user}

***** Please press interactive mode ***** ***** { Circle should be on right side}*****

//Java Program to check Leap Year
package javaBasics.examplesBeginners;

import java.util.Scanner;

public class f
{
    public static void main(String[] args)
    {
        Scanner selectyear=new Scanner(System.in);
        System.out.println("Enter the year");
        int year=selectyear.nextInt();

//  Checking the year whether it is leap year or not
//        you can use this for loop to print the all the leap years and non leap years
//        for(year=2000;year<2020;year++)
//        {
        if(year % 4==0)
        {
            if(year % 100==0)
            {
                if(year % 400==0)
                {
                    System.out.println("The year"+" "+year+ " "+"is leap year");
                }
                else
                {
                        System.out.println("The year"+" "+year+" "+"is not a leap year");
                }

            }
            else
                {
                System.out.println("The year"+" "+year+" "+"is a leap year");
            }
        }
        else
        {
            System.out.println("The year"+" "+year+" "+"is not a leap year");
        }

    }

//    }
}
Output
Enter the year
2000
The year 2000 is a leap year

Enter the year
1999
The year 1999 is not a leap year

6.1Java Program to Check the leap years from the given range

Use of Scanner class {to take input from user}

***** Please press interactive mode ***** ***** { Circle should be on right side}*****

//Java Program to check Leap Year
package javaBasics.examplesBeginners;

import java.util.Scanner;

public class f
{
    public static void main(String[] args)
    {
//        declaring the variables
        int startYear,endYear,i;

        Scanner selectyear=new Scanner(System.in);

//      Enter the starting year
        System.out.println("Enter the startingYear");
        startYear=selectyear.nextInt();
//      Enter the ending year
        System.out.println("Enter the endingYear");
        endYear=selectyear.nextInt();

        System.out.println("The Leap years are: ");

//  Checking the year whether it is leap year or not
//        you can use this for loop to print the all the leap years from starting to ending
//       remember the logic
        for(i=startYear;i<=endYear;i++)
        {
            if((0 == i % 4) && (0 != i  % 100)|| (0 == i % 400))
            {
                System.out.print(i+",");
            }

        }

    }
}
Output
Enter the startingYear
2000
Enter the endingYear
2020
The Leap years are: 
2000,2004,2008,2012,2016,2020,

Enter the startingYear
2020
Enter the endingYear
2030
The Leap years are: 
2020,2024,2028,

7.Java Program to Check the given input character is vowel or consonant

Use of Scanner class {to take input from user}

***** Please press interactive mode ***** ***** { Circle should be on right side}*****

//Program to check Vowel or Consonant
package javaBasics.examplesBeginners;

import java.util.Scanner;

public class g
{
    public static void main(String[] args)
    {
         Scanner scanner = new Scanner(System.in);
        System.out.println("Enter the character");
//.charAt(0) is important
        char ch=scanner.next().charAt(0);
        if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u' || ch=='A' || ch=='E' || ch=='I' || ch=='O' || ch=='U')
        {
            System.out.println("The letter"+" "+ch+" "+"is vowlel");
        }
        else
        {
            System.out.println("The letter"+" "+ch+" "+ "is consonant");
        }

    }
}
Output
Enter the character
a
The letter a is vowel

Enter the character
b
The letter b is consonant

8.Java Program to Swap two numbers using temporary variable

Use of Scanner class {to take input from user}

***** Please press interactive mode ***** ***** { Circle should be on right side}*****

//Swap two numbers using temporary variable
package javaBasics.examplesBeginners;

import java.util.Scanner;

public class h 
{
    public static void main(String[] args) 
    {
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter the num 1");
        int num1 = scan.nextInt();
        System.out.println("Enter the num 2");
        int num2 = scan.nextInt();
        
//      Swapping operation 
        System.out.println("-----Before swapping-----");
        System.out.println(num1+","+num2);

//      Remember this logic
        int temp=num1;
        num1=num2;
        num2=temp;


        System.out.println("-----after swapping-----");
        System.out.println(num1+","+num2);

    }
}
Output
Enter the num 1
10
Enter the num 2
20
-----Before swapping-----
10,20
-----after swapping-----
20,10

Enter the num 1
30
Enter the num 2
40
-----Before swapping-----
30,40
-----after swapping-----
40,30

9.Java Program to Find Largest Among three numbers

Use of Scanner class {to take input from user}

***** Please press interactive mode ***** ***** { Circle should be on right side}*****

//Find Largest Among three numbers
package javaBasics.examplesBeginners;

import java.util.Scanner;

public class i
{
    public static void main(String[] args)
    {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter the num1");
        int a=scanner.nextInt();
        System.out.println("Enter the num2");
        int b=scanner.nextInt();
        System.out.println("Enter the num3");
        int c=scanner.nextInt();

//  Checking the largest among 3 numbers
        if(a>b && a>c)
        {
            System.out.println("The number"+" "+a+" "+"is greaterthan"+" "+b+","+c);
        }
        else if(a<b && b>c)
        {
            System.out.println("The number"+" "+b+" "+"is greaterthan"+" "+a+","+c);
        }
        else if(a<c && b<c)
        {
            System.out.println("The number"+" "+c+" "+"is greaterthan"+" "+a+","+b);
        }
    }
}
Output
Enter the num1
10
Enter the num2
20
Enter the num3
30
The number 30 is greaterthan 10,20

10.Java Program to Check Prime Number

Use of Scanner class {to take input from user}

***** Please press interactive mode ***** ***** { Circle should be on right side}*****

//Program to Check Prime Number
package javaBasics.examplesBeginners;

import java.util.Scanner;

public class j
{
    public static void main(String[] args)
    {

//  Boolean flag is used to chek the condition is true or not
        boolean flag=false;
        Scanner scan=new Scanner(System.in);
        System.out.println("Enter the number");
        int num= scan.nextInt();

//  Remember the logic to check if the given number is prime or not
        for(int i=2;i<num/2;i++)
        {
            if(num % i==0)
            {
                flag=true;
            }

        }
        if(!flag)
        {
            System.out.println("The number is prime number");
        }
        else
        {
            System.out.println("The number is not prime number");
        }
    }
}
Output
Enter the number
2
The number is prime number

Enter the number
6
The number is not prime number

11.Java Program to check the Number is Positive or Negative

Use of Scanner class {to take input from user}

***** Please press interactive mode ***** ***** { Circle should be on right side}*****

//Number is Positive or Negative
package javaBasics.examplesBeginners;

import java.util.Scanner;

public class k
{
    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);
        System.out.println("Enter the number");
        double num = scan.nextDouble();

//remember the logic to check the number is positive or negative
        if (num > 0.0)
        {
            System.out.println("The number is positive");
        }
        else if(num<0.0)
            {
                System.out.println("The number is negative");
            }

    }
}
Output
Enter the number
10
The number is positive

Enter the number
-1
The number is negative

12.Java Program to Check the given character is Alphabet or not

Use of Scanner class {to take input from user}

***** Please press interactive mode ***** ***** { Circle should be on right side}*****

//Java Program to Check the given character is Alphabet or not
package javaBasics.examplesBeginners;

import java.util.Scanner;

public class l
{
    public static void main(String[] args)
    {
           Scanner scanChar=new Scanner(System.in);
        System.out.println("Enter the Character");
        char ch=scanChar.next().charAt(0);

//  remember this logic to Check the given character is Alphabet or not
        if((ch>='A' && ch <='z') || (ch>='A' && ch<='Z'))

            System.out.println(ch+" "+"The input character is Alphabet");
        else
            System.out.println(ch+" "+"The input character is not a Alphabet");
    }
}
Enter the Character
#
# The input character is not a Alphabet

13.Java Program to the sum of natural numbers

Use of Scanner class {to take input from user}

***** Please press interactive mode ***** ***** { Circle should be on right side}*****

//sum of natural numbers
package javaBasics.examplesBeginners;

import java.util.Scanner;

public class m
{
    public static void main(String[] args)
    {
       Scanner scan=new Scanner(System.in);
        System.out.println("Enter the number");
        int num= scan.nextInt();
        int sum=0;

//  remember the logic to print the sum of n numbers
        for(int i=0;i<=num;i++)
        {
            sum=sum+i;
        }

        System.out.println("The sum of natural numbers is"+" "+sum);
    }
}
Output
Enter the number
10
The sum of natural numbers is 55

Enter the number
100
The sum of natural numbers is 5050

14.Factorial

Use of Scanner class {to take input from user}

***** Please press interactive mode ***** ***** { Circle should be on right side}*****

//factorial number
package javaBasics.examplesBeginners;

import java.util.Scanner;

public class n
{
    public static void main(String[] args) {
        Scanner scan=new Scanner(System.in);
        System.out.println("Enter the number");
        long num= scan.nextLong();
        System.out.println("Enter the factorial");
        long fact= scan.nextLong();

//  Remember the logic of finding the factorial
        for(int i=1;i<=num;i++)
        {
            fact=fact*i;
        }
    /*
          The printf method can be particularly useful when displaying
           multiple variables in one line
           which would be tedious using string concatenation: int convertChartoString = 10; int b = 20;
    */
        System.out.printf("The factorial of number is %d = %d",num,fact);
    }
}
Output
Enter the number
10
Enter the factorial
2
The factorial of number is 10 = 7257600

15.Java printing multiplication table

Use of Scanner class {to take input from user}

***** Please press interactive mode ***** ***** { Circle should be on right side}*****

//printing the multiplication table
package javaBasics.examplesBeginners;

import java.util.Scanner;

public class o
{
    public static void main(String[] args) {
        Scanner scan=new Scanner(System.in);
        System.out.println("Enter the number that you want to multiply");
        int num= scan.nextInt();
        System.out.println("Enter the product number ");
        int j= scan.nextInt();


//Remember the log to print the multiplication tale
        for(int i=1;i<=10;i++){
            System.out.printf("%d * %d = %d\n",num,i,num*i);
        }
    }
}
Output
Enter the number that you want to multiply
10
Enter the product number 
1
10 * 1 = 10
10 * 2 = 20
10 * 3 = 30
10 * 4 = 40
10 * 5 = 50
10 * 6 = 60
10 * 7 = 70
10 * 8 = 80
10 * 9 = 90
10 * 10 = 100

16.Java displaying the set of alphabets, and the set of natural numbers

//Program to displaying the set of alphabets, and the set of natural numbers
package javaBasics.examplesBeginners;

import java.util.Scanner;

public class p
{
    public static void main(String[] args)
    {
//        Displaying the alphabets
        char c;

//  remember the logic to print the set of Alphabets
        for(c='A';c<='Z';c++)
        {
            for (c = 'a'; c <= 'z'; c++)
            {
                System.out.printf(c+" "+"\n");
            }
        }

//  Displaying the natural numbers
        Scanner scan=new Scanner(System.in);
        System.out.println("Enter the starting number");
        int startnum= scan.nextInt();
        System.out.println("Enter the ending number");
        int endnum=scan.nextInt();


//  remember the logic to display the numbers
        for(int i=startnum;i<=endnum;i++)
        {
            System.out.printf(i+" ");
        }
    }
}
Output
The alphabets are:
a b c d e f g h i j k l m n o p q r s t u v w x y z 
***** Enter the starting number *****
1
***** Enter the ending number *****
10
1 2 3 4 5 6 7 8 9 10 

Leave a comment

Your email address will not be published. Required fields are marked *