How to Subtract Two Numbers in Java Using a For Loop
- Home
- subtraction
- How to Subtract Two Numbers in Java Using a For Loop
- On
- By
- 0 Comment
- Categories: subtraction
How to Subtract Two Numbers in Java Using a For Loop
In this tutorial , we will discuss “How to Subtract Two Numbers in Java Using a For Loop”

Performing basic arithmetic operations is a fundamental part of learning any programming language, including Java. In this tutorial, we’ll walk through how to create a simple Java program that subtracts two numbers using a for loop. 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.
How to Subtract Two Numbers in Java
Using a for loop.
Program 1
// subtract two numbers //using a for loop class Subtract_With_Forloop { public static void main(String[] args) { int num1=100; int num2=50; int result=0; //subtracting number 2 from number 1 using for loop for(int i=0; i<num2; i++){ result--; } System.out.println("result of subtraction: "); System.out.println(num1 + "-" + num2 + "=" + result); } }
When the above code is executed, it produces the following result
result of subtraction: 100-50=-50
Explanation
This Java program subtracts one number (num2) from another (num1) using a for loop.
Here’s what’s happening step-by-step:
- Two integers are declared:
num1=100;
num2=50;
2. A variable result is initialized to 0.
3. A for loop runs num2 times (in this case, 50 times).
4. Each time the loop runs, it decreases the value of result by 1 (result–).
5. So after 50 iterations, result becomes -50.
6. Finally, it prints the subtraction result as:
100-50=50
Using a for loop and a method
Program 2
//Calculate subtraction of two numbers using a for loop with method class Subtraction_Twonum_UsingLoop { public static void main(String[] args) { int result; int num_1=150; int num_2=25; result=subtractForMethod(num_1,num_2); System.out.println(num_1+" and " - "+ " + num_2 + " is =" + 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
output : 150 and -25 is=125
Explanation
This program subtracts two numbers using a for
loop and a method in Java.
Step-by-step breakdown:
-
Inside the
main
method:-
Two numbers are defined:
-
num_1=150
num_2=25
-
-
The program calls the method subtractForMethod(num_1,num_2), num_2
)
to do the subtraction. -
The result is then printed.
-
-
The subtractForMethod method does the actual subtraction:
-
It starts with result=a (which is num_1)
-
It runs a
for
loopb
times (which is num_2) -
In each loop, it decreases
result
by 1
So it’s like doing: a – 1 – 1 – 1….. (25 times)
-
-
After the loop, the final result is returned to the
main
method and printed.
Using a for loop, and the Scanner class.
Program 3
//subtract two numbers using a loop import java.util.Scanner; class Subtraction_Twonum_UsingLoop { public static void main(String[] args) { Scanner input=new Scanner(System.in); System.out.println("Input value for num1:"); int num1=input.nextInt(); System.out.println("Input value for num2:"); int num2=input.nextInt(); System.out.println("Subtraction of two numbers using a for loop:"); int result=num1; 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
Input value for num1: 35 Input value for num2: 20 Subtraction of two numbers using a for loop: 35+20=15
Explanation
This Java program subtracts two numbers using a for loop, and it lets the user enter the numbers using the Scanner class.
Step-by-step breakdown:
-
User Input:
-
The program asks the user to enter two numbers:
num1 (the number to subtract from
- num2 (the number to subtract)
-
-
Subtraction Logic:
-
It starts by setting result = num1
-
Then, a for loop runs num2 times
-
In each loop,
result
is decreased by 1 (result–) -
So the loop is subtracting 1 repeatedly, num2 times
-
-
Output:
-
It prints the subtraction result
⚠️ However, there’s a mistake in the output line:System.out.println(num1 + "+" + num2 + "=" + result);
This line wrongly shows a plus"+"
sign instead of a minus"-"
.
-
Using a loop inside a method
Program 4
//subtract two numbers using a loop import java.util.Scanner; class Subtraction_Twonum_UsingLoop { public static void main(String[] args) { Scanner input=new Scanner(System.in); System.out.println("Input value for num1:"); int num1=input.nextInt(); System.out.println("Input value for num2:"); int num2=input.nextInt(); int result=subtractForMethod(num1,num2); System.out.println("Subtraction of two numbers using a loop:"); 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
Input value for num1: 100 Input value for num2: 10 Subtraction of two numbers using a loop: 100-10=90
Explanation
This Java program subtracts two numbers using a loop inside a method, and it takes input from the user.
Step-by-step Breakdown:
-
User Input:
The program uses the Scanner
class to take two numbers from the user:
- num1- the number to subtract from
- num2 – the number to subtract
-
Calling a Method:
-
The program calls a method named
subtractForMethod(num1, num2)
to perform the subtraction.
-
-
Subtraction Using a Loop:
-
Inside the method:
-
It sets
result = a
(starting with the first number) -
Then, it runs a
for
loopb
times -
Each time, it decreases
result
by 1 (result–) -
So it’s like doing: a – -1 – 1 – 1…. repeated
b
times
-
-
-
Final Output:
-
After the loop finishes, the method returns the result.
-
The main method prints the final subtraction result in the format:
num1 – num2=result
-
Example:
If the user enters:
- num1=100;
- num2=30
Then the loop will subtract 1 from 100 a total of 30 times.
The result will be 70
, and the output will be: