In this tutorial, we will discuss Java code to Addition Subtraction, Multiplication and division
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
5 method to sum of two numbers
JavaScript program to sum of two numbers
Addition of two numbers in Java using method
Addition of two numbers in PHP using Function
Suggested for you
If statements in Java language
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…
Program to count vowels, consonants, words, characters and space in Java In this article, we…
How to print multiplication table using Array in C++ language In this post, we will…
C Program to multiplication table using Array In this tutorial , we will discuss about…
Java program to check odd or even using recursion In this tutorial, we discuss a…
View Comments
awesome program to start with
Love your blog, do you have a YouTube Channel as well?
No, but I will create a youtube channel very soon