Basic Calculation Based Java Programs

In Java, coding is quite simple. This blog is all about basic programs. , In the first program, we specify the value of both the numbers in the program itself. The second programs take both the numbers (entered by the user) and prints the sum. In the third program, we will add 3 numbers in Java. The fourth example shows the subtraction of two numbers. In the fifth program, multiplication of two numbers is depicted. For sixth program example, division of two number is depicted. In the seventh example, subtraction of two numbers using scanner class is depicted. In the Eighth Example, Multiplication of Two Numbers Using Scanner is depicted. Ninth Example: Division of Two Numbers Using Scanner is depicted.

First Example: Sum of two numbers

public class SumTwoNumbers {

   public static void main(String[] args) {   
        
      int number1 = 15, number2 = 25, sum;  

// Variable declaration and initialization takes place,

      sum = number1 + number2; 

// Here the value of number 1 and number 2 gets stored in sum variable.


      System.out.println("Sum of two numbers: "+sum);

 // This line prints the results
   }
}

Output of the Program

Sum of two numbers: 40

Second Example: Sum of Two Numbers Using Scanner

In the java. util package, Scanner class in Java is found. With this Scanner class, the input from the keyboard is taken. Herewith the help of Java Scanner class, the input is broken into tokens.

The Java Scanner class breaks the input into tokens using a delimiter which is whitespace by default. It provides many methods to read and parse various primitive values. With the help of the scanner, the user can take input so that we can get the values of both the numbers from the user. The program then calculates the sum and displays it.

import java.util.Scanner;
public class SumTwoNumbers2 {

    public static void main(String[] args) {
        
        int number1, number2, sum; // Here only variable are declared
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the First Number: "); // Displaying the text
        number1 = sc.nextInt();
        
        System.out.println("Enter the Second Number: ");// displaying the text
        number2 = sc.nextInt();
        
        sc.close();
        sum = number1 + number2;   //Here the sum is stored in variable Sum
        System.out.println("Sum of two numbers: "+sum);
    }
}

Output: of the program

Enter The First Number: 91

Enter Second Number:  9

Sum of two numbers: 100

Third Example: Sum of Three Numbers

public class SumThreeNumbers {

   public static void main(String[] args) {   
        
      int number1 = 25;
int number2 = 55;
int number3=50;
int sum; 

 // Variable declaration and initialization takes place,

      sum = number1 + number2+number3; 

// Here the value of number1 and number2 gets stored in sum variable.

      System.out.println("Sum of three numbers: "+sum); 

// This line prints the results
   }
}

Output of the Program

Sum of two numbers: 130

Also Read A Beginner’s Guide to Java and its Evolution

Fourth Example: Subtraction of Two Numbers

public class SubTwoNumbers {

   public static void main(String[] args) {   
        
      int number1 = 50, number2 = 25, subtraction;  

// Variable declaration and initialization takes place,

      subtraction = number1 - number2; 

// Here the value of subtraction of two number1 and number2 gets stored in subtraction variable.


      System.out.println("Subtraction of two numbers: "+subtraction);

 // This line prints the results
   }
}
Subtraction of two numbers: 25

Fifth Example: Multiplication of Two Numbers

public class MultiplyTwoNumbers {

   public static void main(String[] args) {   
        
      int number1 = 5;
int number2 = 5;

int multiply;
 // Variable declaration and initialization takes place,

     multiply = number1*number2;
// Here the value of number1 and number2 gets stored in multiply variable.

      System.out.println("Multiplication of two numbers: "+multiply); 

// This line prints the results
   }
}

Output of the Program

Multiplication of two numbers: 25

Sixth Example: Division of Two Numbers

public class DivisionofNumbers {

   public static void main(String[] args) {   
        
      int number1 = 25;
int number2 = 5;

int div; 

 // Variable declaration and initialization takes place,

     div = number1/number2; 

// Here the value of number1 and number2 gets stored in div variable.

      System.out.println("Division of  numbers: "+div); 

// This line prints the results
   }
}

Output of the Program

Sum of two numbers: 5

Seventh Example: Subtraction of Two Numbers Using Scanner

import java.util.Scanner;
public class SubtractionTwoNumbers2 {

    public static void main(String[] args) {
        
        int number1, number2, sub; // Here only variable are declared
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the First Number: "); // Displaying the text
        number1 = sc.nextInt();
        
        System.out.println("Enter the Second Number: ");// displaying the text
        number2 = sc.nextInt();
        
        sc.close();
        sub = number1- number2;   //Here the result is stored in variable sub
        System.out.println("Subtraction of two numbers: "+sub);
    }
}

Output: of the program

Enter The First Number: 100

Enter Second Number:  9

Sum of two numbers: 91

Eighth Example: Multiplication of Two Numbers Using Scanner

import java.util.Scanner;
public class MultiplicationTwoNumbers2 {

    public static void main(String[] args) {
        
        int number1, number2, multiplication; // Here only variable are declared
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the First Number: "); // Displaying the text
        number1 = sc.nextInt();
        
        System.out.println("Enter the Second Number: ");// displaying the text
        number2 = sc.nextInt();
        
        sc.close();
        multiplication = number1*number2;   //Here the result is stored in variable multiplication
        System.out.println("Multiplication of two numbers: "+multiplication);
    }
}

Output: of the program

Enter The First Number: 100

Enter Second Number:  20

Sum of two numbers: 2000

Ninth Example: Division of Two Numbers Using Scanner

import java.util.Scanner;
public class DivTwoNumbers2 {

    public static void main(String[] args) {
        
        int number1, number2, div; // Here only variable are declared
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the First Number: "); // Displaying the text
        number1 = sc.nextInt();
        
        System.out.println("Enter the Second Number: ");// displaying the text
        number2 = sc.nextInt();
        
        sc.close();
        div = number1/number2;   //Here the result is stored in variable div
        System.out.println("division of two numbers: "+div);
    }
}

Output: of the program

Enter The First Number: 27

Enter Second Number:  9

Sum of two numbers:3
Share on:

Leave a Comment