Subtraction in java – Java Program for Addition Subtraction Multiplication and Division of Two Numbers

Subtraction in java: Our website provided core java programs examples with output aid beginners and expert coders to test their knowledge gap and learn accordingly.

Java Program for Addition Subtraction Multiplication and Division of Two Numbers

  • Java program to add, subtract, multiply and divide two numbers using arithmetic operators.

In this java program, we have to perform addition, subtraction, multiplication and division of two given numbers and print the result in screen. There are five fundamental binary arithmetic operators available in Java (+), (-), (*), (/) and (%). All above mentioned arithmetic operators compute the result of specific arithmetic operation and returns its result.

Operator Description Syntax Example
+ Adds two numbers a + b 15 + 5 = 20
Subtracts two numbers a – b 15 – 5 = 10
* Multiplies two numbers a * b 15 * 5 = 75
/ Divides numerator by denominator a / b 15 / 5 = 3
% Returns remainder after an integer division a % b 15 % 5 = 0

Java Program for addition, subtraction, multiplication and division of two numbers

In this java program, we first take two numbers as input from user and store it in variable “a” and “b” and then perform addition, subtraction, multiplication, division and modulus operations and stores results in ‘sum’, ‘difference’ , ‘product’, ‘quotient’ and ‘modulo’ variables respectively. Finally, we print the result of all arithmetic operations on screen.
Java Program for addition, subtraction, multiplication and division of two numbers

package com.tcc.java.programs;
 
import java.util.Scanner;
 
/**
 * Java Program for addition, subtraction, multiplication and division of two
 * numbers
 */
public class ArithmeticOperations {
    public static void main(String[] args) {
        int a, b, sum, difference, product, modulo;
        float quotient;
 
        Scanner scanner;
        scanner = new Scanner(System.in);
        // Take two numbers as input from user
        System.out.println("Enter Two Integers");
        a = scanner.nextInt();
        b = scanner.nextInt();
 
        /* Adding two numbers */
        sum = a + b;
        /* Subtracting two numbers */
        difference = a - b;
        /* Multiplying two numbers */
        product = a * b;
        /* Dividing two numbers by typecasting one operand to float */
        quotient = (float) a / b;
        /* returns remainder of after an integer division */
        modulo = a % b;
 
        System.out.println("Sum : " + sum);
        System.out.println("Difference : " + difference);
        System.out.println("Product : " + product);
        System.out.println("Quotient : " + quotient);
        System.out.println("Remainder : " + modulo);
    }
}

Output

Enter Two Integers
8 4
Sum : 12
Difference : 4
Product : 32
Quotient : 2.0
Remainder : 0