In this tutorial you will learn about the Java Program to Perform Addition, Subtraction, Multiplication and Division and its application with practical example.
Java Program to Perform Addition, Subtraction, Multiplication, and Division
In this tutorial, we will learn to create a Java program that will Addition, Subtraction, Multiplication, and Division using Java programming.
Prerequisites
Before starting with this tutorial, we assume that you are the best aware of the following java programming topics:
- Operators in Java Programming.
- Basic Input and Output function in Java Programming.
- Basic Java programming.
- Arithmetic operations in Java Programming.
Program to perform the arithmetic operations:-
In this program, we will first take two numbers in input from the user. Then will use the arithmetic operators to perform the operations on that numbers. At last, we will print the output to the user. Using the print function.
The operators are as follows:-
- Addition Operator. “+”
- Subtraction Operator. “-“
- Multiplication Operator. “*”
- Divide Operator. “/”
Algorithm:-
1 2 3 4 5 6 7 8 9 10 11 |
1. Declaring the variables for the program. 2. Taking the input numbers from the user. 3. Passing those numbers to the arithmetic operations. 4. Add, subtract, multiply, divide the numbers. 5. Printing the result number. 6. End Program. |
Program to Add Subtract Multiply Divide:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
//Importing the scanner class fro mthe util package import java.util.Scanner; //Main class body public class Cal { //BOdy of the main function of the program. public static void main(String[] args) { //Declaring the required variables for the program. int m, n, opt, add, sub, mul; double div; //Creating the object of the scanner class for taking the input Scanner s = new Scanner(System.in); //Taking the input two numbers from the user. //Taking the input first numbers from the user. System.out.print("Enter first number:"); m = s.nextInt(); //Taking the input second numbers from the user. System.out.print("Enter second number:"); n = s.nextInt(); //Performing the addtion of the two numbers. add = m + n; //Printing the Sum of two numbers. System.out.println("Result:"+add); //Performing the subtraction of the two numbers. sub = m - n; //Printing the Difference of two numbers. System.out.println("Result:"+sub); //Performing the multiplication of the two numbers. mul = m * n; //Printing the Multiplication of two numbers. System.out.println("Result:"+mul); //Performing the division of the two numbers. div = (double)m / n; //Printing the Division of two numbers. System.out.println("Result:"+div); } } |
Output:-
In the above program, we have first initialized the required variable.
- m = it will hold the integer value.
- n = it will hold the integer value.
Input number from the user.
Program Logic Code to perform the calculation.
Printing the addition of the program.