Java Program: Subtract Two Numbers Using Loops(Examples)
- Home
- subtraction
- Java Program: Subtract Two Numbers Using Loops(Examples)
- On
- By
- 0 Comment
- Categories: subtraction
Java Program: Subtract Two Numbers Using Loops(Examples)
Java Program: Subtract Two Numbers Using Loops(Examples)
In this tutorial, we will discuss “Java Program: Subtract Two Numbers Using Loops(Examples).”

Subtract Two Numbers
In this tutorial, we’ll walk through how to create a simple Java program that subtracts two numbers using the loops(for, while and so on). While subtraction is typically a straightforward operation, using a loop introduces a slightly different approach that helps reinforce the use of iterative logic in Java. Whether you’re a beginner or brushing up on your Java skills, this example will help you understand how control structures like loops can be used creatively for basic tasks.
Subtract two numbers in Java using for loops
Program 1
// subtract two numbers
//using a for loop
class Subtract_Twonum {
public static void main(String[] args) {
int num1=100;
int num2=50;
int result=0;
for(int i=0; i<num2; i++){
result--;
}
System.out.println(num1 + "-" + num2 + "=" + result);
}
}
When the above code is executed, it produces the following result
100+50=50
Subtract two numbers in Java using for loops with the method
Program 2
//subtract two numbers using a loop
class Subtraction_Twonum_UsingLoop {
public static void main(String[] args) {
int result;
int num1=150;
int num2=25;
result=subtractForMethod(num1,num2);
System.out.println("Subtraction of two numbers using a for loop with method:" ) ;
System.out.println(num1 + "-" + num2 + "=" + result);
}
public static int subtractForMethod(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 for loop with method: 150-25=125
Subtract two numbers in Java using while loops
Program 3
//subtract two numbers using while loop
//import java.util.Scanner;
class Subtraction_Twonum_UsingwhileLoop {
public static void main(String[] args) {
int num1=75;
int num2=35;
int res=subtract_While_Method(num1,num2);
//calling the user drfined method for output
System.out.println("Subtraction of two numbers using while loop with method:" ) ;
System.out.println(num1 + "-" + num2 + "=" + res);
}
public static int subtract_While_Method(int a, int b){
int result=a;
int i=0;
while( i<b){
result--;
i++;
}
return result;//result return to main method
}
}
When the above code is executed, it produces the following result
Subtraction of two numbers using while loop with method: 75-35=40
Subtract two numbers in Java using while loops with method
Program 4
//subtract two numbers using while loop with method and user input
import java.util.Scanner;
class Subtract_Twonum_Using_whileLoop {
public static void main(String[] args) {
Scanner input_num=new Scanner(System.in);
System.out.println("Input number to num1:");
int num1=input_num.nextInt();
System.out.println("Input number to num2:");
int num2=input_num.nextInt();
int res=subtract_While_Method(num1,num2);
//calling the user drfined method for output
System.out.println("Subtraction of two numbers using while loop with method and user input:" ) ;
System.out.println(num1 + "-" + num2 + "=" + res);
}
public static int subtract_While_Method(int a, int b){
int result=a;
int i=0;
while( i<b){
result--;
i++;
}
return result;//result return to main method
}
}
When the above code is executed, it produces the following result
Input number to num1: 125 Input number to num2: 35 Subtraction of two numbers using while loop with method and user input: 125-35=90
Subtract two numbers in Java using do-while loops
Program 5
//how to subtract two numbers using do-while loop
class Subtraction_Twonum_UsingForLoop {
public static void main(String[] args) {
int num1=45;
int num2=25;
//Subtraction of two numbers using a do-while loop
int result=num1;
int i=0;
do{
result--;
i++;
}while(i<num2);
System.out.println("Result is: "+num1 + "-" + num2 + "=" + result);
}
}
When the above code is executed, it produces the following result
Result is: 45-25=20
Subtract two numbers in Java using do-while loops with user-input
Program 6
//how to subtract two numbers using do-while loop
import java.util.Scanner;
class Subtraction_Twonum_UsingForLoop {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.println("Input value first number:");
int num1=input.nextInt();
System.out.println("Input second number:");
int num2=input.nextInt();
System.out.println("Subtraction of two numbers:");
//Subtraction of two numbers using a do-while loop
int result=num1;
int i=0;
do{
result--;
i++;
}while(i<num2);
System.out.println(num1 + "-" + num2 + "=" + result);
}
}
When the above code is executed, it produces the following result
Input value first number: 56 Input second number: 67 Subtraction of two numbers: 56+67=-11
Related post
10 ways to add two numbers in Java
20 ways to subtract two numbers in Java
Subtraction of two numbers in Java using recursion
Subtraction of two numbers in PHP
Addition of two numbers using pascal in various ways
Pascal Code for Subtracting Two Numbers Using Various Methods