Codeforcoding

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 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

Sum of two numbers in Java

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

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
Exit mobile version