Java Program to Perform Arithmetic Operations

Java Arithmetic Operators are used to perform basic mathematical operations on numerical data types, such as integers and floating-point numbers. These operators can be classified into two categories: unary and binary operators. The most common arithmetic operators are addition (+), subtraction (-), multiplication (*), division (/), and Modulus (%).

Problem Description

Write a Java program to perform all arithmetic operations.

Arithmetic Operators in Java:

  1. Addition Operator (+)
  2. Subtraction Operator (-)
  3. Multiplication Operator (*)
  4. Division Operator (/)
  5. Modulus Operator (%)

1. Addition (+): The addition operator adds two operands together.

Example:

int x = 12;
int y = 45;
int sum = x + y; // sum is now 57

2. Subtraction (-): The subtraction operator subtracts the second operand from the first.

Example:

advertisement
advertisement
int x = 50;
int y = 30;
int diff = x - y; // diff is now 20

3. Multiplication (*): The multiplication operator multiplies two operands.

Example:

int x = 56;
int y = 42;
int mul = x * y; // mul is 2352

4. Division (/): The division operator divides the first operand by the second.

Note: Join free Sanfoundry classes at Telegram or Youtube

Example:

int x = 30;
int y = 5;
int div = x / y; // div is 6

5. Modulus (%): The modulus operator returns the remainder of the division operation.

Example:

advertisement
int x = 30;
int y = 4;
int remainder = x % y;
Problem Solution

Enter two integers as input. After that we use switch case to choose from different arithmetic operations like addition, subtraction, multiplication, division and modulus to get the desired output.

Program/Source Code

Here is the source code of the Java Program to Illustrate the Use of Arithmetic Operators. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

/*
 * Java Program to Perform Arithmetic Operations
 */
 
import java.util.Scanner;
public class Arithmetic_Operators
{
   public static void main(String args[])
   {   
        Scanner s = new Scanner(System.in);
        while(true)
        {
            System.out.println("");
            System.out.println("Enter the two numbers to perform operations ");
            System.out.print("Enter the first number : ");
            int x = s.nextInt();
            System.out.print("Enter the second number : ");
            int y = s.nextInt();
            System.out.println("Choose the operation you want to perform ");
            System.out.println("Choose 1 for ADDITION");
            System.out.println("Choose 2 for SUBTRACTION");
            System.out.println("Choose 3 for MULTIPLICATION");
            System.out.println("Choose 4 for DIVISION");
            System.out.println("Choose 5 for MODULUS");
            System.out.println("Choose 6 for EXIT");
            int n = s.nextInt();
            switch(n)
            {
                case 1:
                int add;
                add = x + y;
                System.out.println("Addition of Two Numbers : "+add);
                break;
 
                case 2:
                int sub;
                sub = x - y;
                System.out.println("Subtraction of Two Numbers : "+sub);
                break;
 
                case 3:
                int mul;
                mul = x * y;
                System.out.println("Multiplication of Two Numbers  : "+mul);
                break;
 
                case 4:
                float div;
                div = (float) x / y;
                System.out.print("Division of Two Numbers  : "+div);
                break;
 
                case 5:
                int mod;               
                mod = x % y;
                System.out.println("Modulus of Two Numbers  : "+mod);
                break;
 
                case 6:
                System.exit(0);
            }
        }
    }
}
Program Explanation

1. It imports the Scanner class from the java.util package.
2. It creates an instance of the Scanner class called s to get input from the user.
3. It uses a while loop with a condition of true to continuously prompt the user for input until they choose to exit the program.
4. It prints out instructions to the user to enter two numbers and select an arithmetic operation to perform.
5. It uses the nextInt() method of the Scanner class to read in two integers entered by the user.
6. It uses a switch statement to perform the selected arithmetic operation and display the result to the user.
7. The program uses x, y, add, sub, mul, div and mod variables to store the input values and the result of each arithmetic operation.

advertisement
Runtime Testcases

Test case 1 (Addition): In this case, the values “12” and “45” were entered, and we will execute the addition operation.

$ javac Arithmetic_Operators.java
$ java Arithmetic_Operators
 
Enter the two numbers to perform operations 
Enter the first number : 12
Enter the second number : 45
Choose the operation you want to perform 
Choose 1 for ADDITION
Choose 2 for SUBTRACTION
Choose 3 for MULTIPLICATION
Choose 4 for DIVISION
Choose 5 for MODULUS
Choose 6 for EXIT
1
Addition of Two Numbers : 57

Test case 2 (Subtraction): The values “50” and “30” were entered as input for this case, and we will perform the subtraction operation.

$ javac Arithmetic_Operators.java
$ java Arithmetic_Operators
 
Enter the two numbers to perform operations 
Enter the first number : 50
Enter the second number : 30
Choose the operation you want to perform 
Choose 1 for ADDITION
Choose 2 for SUBTRACTION
Choose 3 for MULTIPLICATION
Choose 4 for DIVISION
Choose 5 for MODULUS
Choose 6 for EXIT
2
Subtraction of Two Numbers : 20

Test case 3 (Multiplication): In this case, the numbers “56” and “42” were entered as input, and the multiplication operation will be performed.

$ javac Arithmetic_Operators.java
$ java Arithmetic_Operators
 
Enter the two numbers to perform operations 
Enter the first number : 56
Enter the second number : 42
Choose the operation you want to perform 
Choose 1 for ADDITION
Choose 2 for SUBTRACTION
Choose 3 for MULTIPLICATION
Choose 4 for DIVISION
Choose 5 for MODULUS
Choose 6 for EXIT
3
Multiplication of Two Numbers : 2352

Test case 4 (Division): In this case, the numbers “30” and “5” were entered as input, and the division operation will be performed.

$ javac Arithmetic_Operators.java
$ java Arithmetic_Operators
 
Enter the two numbers to perform operations 
Enter the first number : 30
Enter the second number : 5
Choose the operation you want to perform 
Choose 1 for ADDITION
Choose 2 for SUBTRACTION
Choose 3 for MULTIPLICATION
Choose 4 for DIVISION
Choose 5 for MODULUS
Choose 6 for EXIT
4
Division of Two Numbers : 6

Test case 5 (Modulus): In this case, the numbers “30” and “5” were entered as input, and the modulus operation will be performed.

$ javac Arithmetic_Operators.java
$ java Arithmetic_Operators
 
Enter the two numbers to perform operations 
Enter the first number : 30
Enter the second number : 4
Choose the operation you want to perform 
Choose 1 for ADDITION
Choose 2 for SUBTRACTION
Choose 3 for MULTIPLICATION
Choose 4 for DIVISION
Choose 5 for MODULUS
Choose 6 for EXIT
5
Modulus of Two Numbers : 2

To practice programs on every topic in Java, please visit “Programming Examples in Java”, “Data Structures in Java” and “Algorithms in Java”.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.