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.
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. “/”
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:-
//Importing the scanner class fro mthe util package
//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;
//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.
//Performing the subtraction of the two numbers.
sub = m - n;
//Printing the Difference of two numbers.
//Performing the multiplication of the two numbers.
mul = m * n;
//Printing the Multiplication of two numbers.
//Performing the division of the two numbers.
div = (double)m / n;
//Printing the Division of two numbers.
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.