Java Program for Addition Subtraction Multiplication and Division of Two Numbers

Here is a 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.

package com.tcc.java.programs;

import java.util.Scanner;

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

Recommended Posts
Java Program to Find LCM and GCD of Two Numbers
Java Program to Find All Factors of a Number
Java Program to Check a Number is Prime Number or Not
Java Program to Find Sum of Digits of a Number
Java Program to Check If a Year is Leap Year or Not
Java Program to Check Whether a Number is Palindrome or Not
Java Program to Check Whether a Number is Positive or Negative
Java Program to Convert Decimal to Binary Numbers
Java Program to Find Factorial of a Number using For Loop
Java Program To Reverse Digits of a Number using While Loop
Java Program to Print Armstrong Numbers Between 1 to N
All Java Programs