Table of Contents
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.
here, I’ll give you separate short programs for each method so it’s easier to understand
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
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
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
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
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
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
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 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
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…
10 ways to subtract two numbers in Java In this article, we will discuss the…
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.