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
Suggested for you
If statements in Java language
PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…
PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…
5 methods to add two numbers in Java In this tutorial, we will discuss the…
Python full Pyramid star pattern program In this tutorial, we will discuss the concept of…
Write a function or method to convert C into F -Entered by the user In…
How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…
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