Table of Contents
In this article, we will discuss the concept of Java Code Examples – 10 ways to subtract two numbers in Java
In this post, we are going to learn how to write a program to 10 Ways to Subtract Two Numbers (With Examples) in the Java programming language.
here, I’ll give you separate short programs for each method so it’s easier to understand
Method 1
int result=num1 - num2
Example 1
//Subtract two numbers in Java
// Using subtraction operator
class HelloWorld {
public static void main(String[] args) {
int number1=34;
int number2=26;
int sub=number1 - number2;
System.out.println("The subtraction of "+number1+" and "+number2+" is :"+sub);
}
} When the above code is executed, it produces the following result
The subtraction of 34 and 26 is :8
Method 2
int result=Math.subtractExact(num_1,num_2);
Example 2
// subtract two numbers
//using Math.subtractExact(); method
import java.lang.Math;
class Math_Subtract_Twonum {
public static void main(String[] args) {
int num_1=50;
int num_2=15;
int result;
result=Math.subtractExact(num_1,num_2);
System.out.println("Result is: "+result);
}
} When the above code is executed, it produces the following result
Result is: 35
Method 3
result=num_1 + ~num_2 + 1;
Example 3
// subtract two numbers
//using bitwise operator
import java.lang.Math;
class Math_Subtract_Twonum {
public static void main(String[] args) {
int num_1=500;
int num_2=150;
int result;
result=num_1 + ~num_2 + 1;
System.out.println("Result is: "+result);
}
} When the above code is executed, it produces the following result
Result is: 350
Method 4
result=subtract_Recursion(num1,num2);
Example 4
// subtract two numbers using recursive method
//import java.lang.Math;
class Recursive_Subtract_Twonum {
public static void main(String[] args) {
int num1=150;
int num2=15;
int result;
result=subtract_Recursion(num1,num2);
System.out.println("Subtraction using recursive method "+result);
}
public static int subtract_Recursion(int a, int b){//recursive method
if(b==0){
return a;
}else{
return subtract_Recursion(a - 1, b - 1);
}
}
}
Method 5
int result=subtract_Method(num1,num2);
Example5
// subtract two numbers using user-defined method
//import java.lang.Math;
class subtract_Twonum_Method {
public static void main(String[] args) {
int num1=55;
int num2=5;
int result;
result=subtract_Method(num1,num2);
System.out.println("Subtraction of two numbers using method "+result);
}
public static int subtract_Method(int a, int b){
return a - b;
}
} When the above code is executed, it produces the following result
Subtraction of two numbers using method 50
Method 6
int result=0;
for(int i=0; i<num2; i++){
result--;
}
result+=num1; Example 6
// subtract two numbers
//using a for loop
import java.lang.Math;
class Subtract_Twonum {
public static void main(String[] args) {
int num1=230;
int num2=150;
int result=0;
for(int i=0; i<num2; i++){
result--;
}
result+=num1;
System.out.println("Result is: "+result);
}
} When the above code is executed, it produces the following result
Result is: 80
method 7
int result=(num_1>num_2) ? num_1 - num_2: num_2 - num_1;
Example 7
// subtract two numbers
//using ternary operator
class ternarySubtract_Twonum {
public static void main(String[] args) {
int num_1=50;
int num_2=35;
int result;
result=(num_1>num_2) ? num_1 - num_2: num_2 - num_1;
System.out.println("Result is: "+result);
}
} When the above code is executed, it produces the following result
Result is: 15
method 8
int result=0;
switch(number2){
case 1:
result= number1-1;
break;
//if you want other cases can be added Example 8
// subtract two numbers
//using the Switch Case statements
class Subtract_Twonum_Switch {
public static void main(String[] args) {
int a=10;
int b=3;
int result=0;
switch(b){
case 1:
result= a-1;
break;
case 2:
result =a-2;
break;
case 3:
result= a-3;
break;
//if you want other cases can be added
default:
result= 0;
}
System.out.println("Result is: "+result);
}
} When the above code is executed, it produces the following result
Result is: 7
method 9
AtomicInteger result=new AtomicInteger(num1); int subtract=result.addAndGet(-num2);
Example 9
// subtract two numbers
//using the AtomicInteger class
import java.util.concurrent.atomic.AtomicInteger;
class AtomicInteger_Subtract_Twonum {
public static void main(String[] args) {
int num_1=120;
int num_2=50;
AtomicInteger sub=new AtomicInteger(num_1);
int result=sub.addAndGet(-num_2);
System.out.println("Result is: "+sub.get());
}
} When the above code is executed, it produces the following result
Result is: 70
method 10
BigDecimal.valueOf(num1).subtract(BigDecimal.valueOf(num2)));
Example 10
// subtract two numbers
//using the BigDecimal class
import java.math.BigDecimal;
class BigDecimal_Subtract_Twonum {
public static void main(String[] args) {
int num_1=20;
int num_2=15;
System.out.println("Result is: "+BigDecimal.valueOf(num_1).subtract(BigDecimal.valueOf(num_2)));
}
} When the above code is executed, it produces the following result
Result is: 5
Similar post
Java program to add two numbers in Java -5 ways
C program to add two numbers -5 ways
C++ program to add two numbers -5 ways
10 ways to add two numbers in pascal
Java program to subtract two numbers using loop
Multiply two numbers in Java using scanner| 5 different ways In this article, we will…
5 Different ways to Divide two numbers in Java using scanner In this article, we…
Learn 8 Ways to Subtract Two Numbers Using Methods in Java In this article, we…
Java Code Examples – Multiply Two Numbers in 5 Easy Ways In this article, we…
How to Divide two numbers in Java| 5 different ways In this article, we will…
5 Ways to Add Two Numbers in Java with User Input (Examples) In this article,…
This website uses cookies.