Alphalearningschool
        

<<< Prev

Next >>>

Up
Java Excersies

CORE JAVA PROGRAMS FOR BEGINEERS

 

1)    Java Program to Get Number Input from User    

To get input from the user in Java programming, first you have to import the java.util.Scanner package that contains Scanner class which helps you to get the input from the user as shown in the following program.

* Java Program Example - Get Integer Input from User */
		
import java.util.Scanner;

public class Inputuser
{
    public static void main(String args[])
    {
        int num;
        Scanner scan = new Scanner(System.in);
		
        System.out.print("Enter a Number : ");
        num = scan.nextInt();
		
        System.out.print("You entered " +num);
     }
}

 

Output:

1a) Get Character Input from User

 

 

import java.util.Scanner;

public class Inputchar
{
    public static void main(String args[])
    {
        char ch;
        Scanner scan = new Scanner(System.in);
		
        System.out.print("Enter a Character : ");
        ch = scan.next().charAt(0);
		
        System.out.print("You entered " +ch);
    }
}
OUTPUT

 

 

1b) Get String Input from User

 

import java.util.Scanner;

public class Inputstr 
{

public static void main(String args[])
    {
        String str;
        Scanner scan = new Scanner(System.in);
		
        System.out.print("Enter String : ");
        str = scan.nextLine();
		
        System.out.print("You entered " +str);
    }
}

 

Output:

2)  Java Program to Print Integers

To print integers in Java Programming, you have to just initialize the integer value to any variable say num and place the variable in the bracket of System.out.println()

 

public class Printint
{
    public static void main(String args[])
    { Printint
	
        int num = 10;
        System.out.println("The value of num is " + num);
		
    }
}

 

Output:

3)  Java Program to Add Two Numbers

To add two numbers in Java Programming, you have to ask to the user to enter the two number and place the addition of the two number in any variable then print the variable's value which is the addition result of the two number.

 

import java.util.Scanner;

public class Addnum
{
    public static void main(String args[])
    {
        int a, b, sum;
        Scanner scan = new Scanner(System.in);
		
        System.out.print("Enter Two Numbers : ");
        a = scan.nextInt();
        b = scan.nextInt();
		
        sum = a + b;
		
        System.out.print("Sum of the Entered Two Number is " +sum);
    }
}

 

Output:

4)  Java Program to Check Even or Odd

To check whether the input number is an even number or an odd number in Java programming, you have to ask to the user to enter the number, now if number is divisible by 2 (it will be even number) and if the number is not divisible by 2 (it will be an odd number)

 

import java.util.Scanner;

public class Evenodd
{
    public static void main(String args[])
    {
        int num;
        Scanner scan = new Scanner(System.in);
		
        System.out.print("Enter a Number : ");
        num = scan.nextInt();
		
        if(num%2 == 0)
        {
            System.out.print("This is an Even Number");
        }
        else
        {
            System.out.print("This is an Odd Number");
        }
    }
}

 

Output:

5)  Java Program to Check Prime or Not

To check whether the input number is a prime number or not a prime number in Java programming, you have to ask to the user to enter the number and start checking for prime number. If number is divisible from 2 to one less than that number, then the number is not prime number otherwise it will be a prime number.

 

import java.util.Scanner;

public class PrimeNumber
{
    public static void main(String args[])
    {
        int num, i, count=0;
        Scanner scan = new Scanner(System.in);
		
        System.out.print("Enter a Number : ");
        num = scan.nextInt();
		
        for(i=2; i<num; i++)
        {
            if(num%i == 0)
            {
                count++;
                break;
            }
        }
        if(count == 0)
        {
            System.out.print("This is a Prime Number");
        }
        else
        {
            System.out.print("This is not a Prime Number");
        }
  
  }

  

Output:

 

 

6) Java Program to   Check Leap Year or Not

To check whether the input year is a leap year or not a leap year in Java Programming, you have to ask to the user to enter the year and start checking for the leap year.

 

import java.util.Scanner;

public class LeapYear
{
    public static void main(String args[])
    {
       int yr;
       Scanner scan = new Scanner(System.in);
	   
       System.out.print("Enter Year : ");
       yr = scan.nextInt();
	   
       if((yr%4 == 0) && (yr%100!=0))
       {
           System.out.print("This is a Leap Year");
       }
       else if(yr%100 == 0)
       {
           System.out.print("This is not a Leap Year");
       }
       else if(yr%400 == 0)
       {
           System.out.print("This is a Leap Year");
       }
       else
       {
           System.out.print("This is not a Leap Year");
       }
    }
}   

 

Output:

 

7)  Java Program to Check Original Equals Reverse or Not

To check whether the original number is equal to its reverse or not in Java programming, you have to ask to the user to enter the number and reverse that number then check that reverse is equal to the original or not, before reversing the number make a variable of the same type and place the value of the original number to that variable to check after reversing the number.

 

import java.util.Scanner;

public class Orginreverse
{
    public static void main(String args[])
    {
       int num, orig, rev=0, rem;
       Scanner scan = new Scanner(System.in);
	   
       System.out.print("Enter a Number : ");
       num = scan.nextInt();
	   
       orig=num;
	   
       while(num>0)
       {
           rem = num%10;
           rev = rev*10 + rem;
           num = num/10;
       }
       if(orig == rev)
       {
           System.out.print("Reverse is Equal to Original");
       }
       else
       {
           System.out.print("Reverse is not Equal to Original");
       }
    }
}

Output:

 

<

8) Java Program to Perform Mathematical Operations

To perform mathematical operations such as addition, subtraction, multiplication and division of any two number in Java Programming, you have to ask to the user to enter the two number and then perform the action accordingly.

 

import java.util.Scanner;

public class Mathops {
	import java.util.Scanner;

public class Mathops {
	
	
	 public static void main(String args[])
	    { 
      ;
     int first, second, add, subtract, multiply;
     float devide;
    Scanner scan = new Scanner(System.in);
	   
    System.out.print("Enter First Number : ");
     first = scan.nextInt();
    System.out.print("Enter Second Number : ");
    second = scan.nextInt();
    
    add = first + second;
    subtract = first - second;
    multiply = first * second;
    devide = (float) first / second;

    System.out.println("Sum = " + add);
    System.out.println("Difference = " + subtract);
    System.out.println("Multiplication = " + multiply);
    System.out.println("Division = " + devide);

}
}


	
	 public static void main(String args[])
	    { 
      ;
     int first, second, add, subtract, multiply;
     float devide;
    Scanner scan = new Scanner(System.in);
	   
    System.out.print("Enter First Number : ");
     first = scan.nextInt();
    System.out.print("Enter Second Number : ");
    second = scan.nextInt();
    
    add = first + second;
    subtract = first - second;
    multiply = first * second;
    devide = (float) first / second;

    System.out.println("Sum = " + add);
    System.out.println("Difference = " + subtract);
    System.out.println("Multiplication = " + multiply);
    System.out.println("Division = " + devide);

}
}

 

Output:

9) Java Program to   Make Calculator

 To make a simple calculator in Java Programming which performs the basic four mathematical operations i.e., addition, subtraction, multiplication, and division, use the switch case to identify the input operator to perform required calculation and then display the result on the screen as shown in the following program.

 

/* Java Program Example - Make Calculator */
		
import java.util.Scanner;

public class Calculator
{
    public static void main(String args[])
    {
        float a, b, res;
        char choice, ch;
        Scanner scan = new Scanner(System.in);
		
        do
        {
            System.out.print("1. Addition\n");
            System.out.print("2. Subtraction\n");
            System.out.print("3. Multiplication\n");
            System.out.print("4. Division\n");
            System.out.print("5. Exit\n\n");
            System.out.print("Enter Your Choice : ");
            choice = scan.next().charAt(0);
            switch(choice)
            {
                case '1' : System.out.print("Enter Two Number : ");
                    a = scan.nextFloat();
                    b = scan.nextFloat();
                    res = a + b;
                    System.out.print("Result = " + res);
                    break;
                case '2' : System.out.print("Enter Two Number : ");
                    a = scan.nextFloat();
                    b = scan.nextFloat();
                    res = a - b;
                    System.out.print("Result = " + res);
                    break;
                case '3' : System.out.print("Enter Two Number : ");
                    a = scan.nextFloat();
                    b = scan.nextFloat();
                    res = a * b;
                    System.out.print("Result = " + res);
                    break;
                case '4' : System.out.print("Enter Two Number : ");
                    a = scan.nextFloat();
                    b = scan.nextFloat();
                    res = a / b;
                    System.out.print("Result = " + res);
                    break;
                case '5' : System.exit(0);
                    break;
                default : System.out.print("Wrong Choice!!!");
                    break;
            }
      System.out.print("\n---------------------------------\n");
        }while(choice != 5);       
    }
}

 

Output:

 

 

 

10)  Java Program to Add Digits of Number

 To add digits of any number in Java Programming,   have to ask to the user to enter the number to add their digits and display the summation of digits of that number.

 

/* Java Program Example - Add Digits of Number */
		
import java.util.Scanner;

public class Adddigitnum
{
    public static void main(String args[])
    {
        int num, rem=0, sum=0, temp;
        Scanner scan = new Scanner(System.in);
		
        System.out.print("Enter a Number : ");
        num = scan.nextInt();
		
        temp = num;
		
        while(num>0)
        {
            rem = num%10;
            sum = sum+rem;
            num = num/10;
        }
		
        System.out.print("Sum of Digits of " +temp+ " is " +sum);        
    }
}

 

Output: