Calculations

Java code to Addition Subtraction,Multiplication and division

Java code to Addition Subtraction, Multiplication and division

In this tutorial, we will discuss Java code to Addition Subtraction, Multiplication and division

Java arithmetic operators

In this post, we will learn about how to perform addition, subtraction multiplication, division of any two numbers using if statements  in Java programming

Java program will request the user to enter two number digits and the user selects an operator to perform the addition subtraction multiplication and division of the entered number by the user and displays the output on the screen
the program will display the result according to the user selection

Program 1

import java.util.Scanner;
class CalculationJava{
public static void main (String args[]){
Scanner scan=new Scanner(System.in);
System.out.print("Enter the first number: ");
int num1=scan.nextInt();
System.out.print("Enter the second number: ");
int num2=scan.nextInt();

System.out.println("\nAddition of "+num1+"+"+num2+" = "+(num1+num2 ));
System.out.println("Substraction of "+num1+"-"+num2+" = "+(num1-num2 ));
System.out.println("Multiplication of "+num1+"x"+num2+" = "+(num1*num2 ));
System.out.print("Division of "+num1+"/"+num2+" = "+(num1/num2 ));
}

}

When the above code is executed, it produces the following results

Enter the first number: 60
Enter the second number: 15

Addition of 60+15 = 75
Substraction 60 - 15 = 45
Multiplication of 60 X 15 = 900
Division of 60/15 = 4

 

Program 2

import java.util.Scanner;
class CalculationJava1{
public static void main (String args[]){
char ch;
Scanner scan=new Scanner(System.in);
System.out.print("Enter the first number: ");
int num1=scan.nextInt();

System.out.print("Enter the second number: ");
int num2=scan.nextInt();

System.out.print("Enter the operator you want(+, -, *, /) :");
ch=scan.next().charAt(0);

if(ch=='+'){
System.out.println("\nAddition of "+num1+"+"+num2+" = "+(num1+num2 ));

}

else if(ch=='-'){
System.out.println("Substraction of "+num1+"-"+num2+" = "+(num1-num2 ));

}

else if(ch=='*'){
System.out.println("Multiplication of "+num1+"x"+num2+" = "+(num1*num2 ));

}
else if(ch=='/'){
System.out.print("Division of "+num1+"/"+num2+" = "+(num1/num2 ));

}

else{
System.out.print("Operator is not available" );
}
}
}

When the above code is executed, it produces the following results

Case 1

Enter the first number: 60
Enter the second number: 15
Enter the operator you want(+, -, *, /):+

Addition of 60+15=75

 

Case 2

Enter the first number: 60
Enter the second number: 15
Enter the operator you want(+, -, *, /):-

Substraction of 60-15=45

 

Case 3

Enter the first number: 60
Enter the second number: 15
Enter the operator you want(+, -, *, /):*
Multiplication of 60*15=900

 

Case 4

Enter the first number: 60
Enter the second number: 15
Enter the operator you want(+, -, *, /):/
Division of 60/15=4

Case 5

Enter the first number: 60
Enter the second number: 15
Enter the operator you want(+, -, *, /):x
Operator is not available

Related program in Other language

Cpp Add Subtract Multiply Divide

C Add Subtract Multiply Divide

Python Addition ,subtract,multiply,division

 

Suggested for you

Operator in Java language

If statements in Java language

Data type in Java language

 

C Program to Addition Subtraction,Multiplication and division
Cpp program to check whether a number is even or odd
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

View Comments

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

1 month ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

2 months ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

10 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

10 months ago