Table of Contents
Learn 8 Ways to Subtract Two Numbers Using Methods in Java
In this article, we will discuss the concept of Java Code Examples – Learn 8 Ways to Subtract Two Numbers Using Methods in Java
In this post, we are going to learn how to write a program to 8 Ways to Subtract of Two Numbers (With Examples) in the Java programming language.

Subtract two numbers in Java
here, I’ll give you separate short programs for each method so it’s easier to understand
Subtracting two numbers Using basic subtraction (-) operator
The simplest and most common way to subtract two numbers
Program 1
// subtract two numbers using - operator
//standard method
//import java.lang.Math;
class Subtraction_Twonum_with_minusOperator {
public static void main(String[] args) {
int result=subtract(35,24);//calling the custom method
System.out.println("Subtraction of two numbers using minus: "+result);
//displaying the result
}
public static int subtract(int a, int b){//usern defined method
return a - b;//return to main method
}
}
When the above code is executed, it produces the following result
Subtraction of two numbers using minus: 11
Subtracting two numbers using addition
Program 2
// subtract two numbers using addition
//import java.lang.Math;
class Subtraction_Twonum_Addition {
public static void main(String[] args) {
int result;
result=subtractUsingAddition(200,50);//calling the custom method
System.out.println("Subtraction of two numbers using addition "+result);
}
public static int subtractUsingAddition(int a, int b){//user defined method for subtraction
return a + (-b);//return to main method
}
}
When the above code is executed, it produces the following result
Subtraction of two numbers using addition 150
Subtracting two numbers using compliment
Program 3
// subtract two numbers using compliment
//import java.lang.Math;
class Subtraction_Twonum_Compliment {
public static void main(String[] args) {
int result;
result=subtract(15,5);//calling the method
System.out.println("Subtraction of two numbers using compliment "+result);
//displaying the output
}
public static int subtract(int a, int b){//user-defined method
return a + (~b) +1;//return to main method
}
}
When the above code is executed, it produces the following result
Subtraction of two numbers using compliment: 10
Subtracting two numbers using Bitwise operator
Program 4
// subtract two numbers with Bitwise operator
class SubtractionTwonumBitwise {
public static void main(String[] args) {
int result;
result=subtract(64,14);//calling the method
System.out.println("Subtraction of two numbers using Bitwise operator "+result);
//displaying the result
}
public static int subtract(int a, int b){//user-defined method
while(b!=0){
int borrow=(~a) & b;
a=a^b;
b=borrow <<1;
}
return a;//return to main method
}
}
When the above code is executed, it produces the following result
Subtraction of two numbers using Bitwise operator 50
Subtracting two numbers using math class
Program 5
// subtract two numbers using Math class
import java.lang.Math;
class Subtraction_Twonum_MathClss {
public static void main(String[] args) {
int result;
result=subtract(24,8);
System.out.println("Subtraction of two numbers using Math class: "+result);
}
public static int subtract(int a, int b){
//user defined method
return Math.subtractExact(a,b) ;
}
}
When the above code is executed, it produces the following result
Subtraction of two numbers using Math class: 16
Subtract two numbers using recursion
Program 6
//subtract two numbers using recursion
class Subtraction_Twonum_Usingrecursion {
public static void main(String[] args) {
int result;
result=subtract(150,50);
System.out.println("Subtraction of two numbers using recursion: "+result);
}
public static int subtract(int a, int b){
if(b==0){
return a;
}else{
return subtract (a-1,b-1);
}
}
}
When the above code is executed, it produces the following result
Subtraction of two numbers using recursion: 100
Subtraction of two numbers using loop
Program 7
//subtract two numbers using a loop
class Subtraction_Twonum_usingLoop {
public static void main(String[] args) {
int result;
result=subtract(100,60);
System.out.println("Subtraction of two numbers using a loop: "+result);
}
public static int subtract(int a, int b){
int result=a;
for(int i=0; i<b; i++){
result--;
}
return result;
}
}
When the above code is executed, it produces the following result
Subtraction of two numbers using a loop: 40
Program 8
Subtraction of two numbers using while loop
//Subtraction of two num Using method
class SubTwoNumUsingMethodWhile {
public static int subTwoNum(int a,int b){
//user defined method using while
while(b>0){
a--;
b--;
}
return a;
}
public static void main(String[] args) {
int sub=subTwoNum(55,34);
System.out.println("Subtraction of given numbers : "+ sub);
}
}
When the above code is executed, it produces the following result
Subtraction of given numbers : 21
Similar post
Subtraction of two numbers using 10 ways in Java
Subtraction of two numbers using 4 ways in JavaScript
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