Table of Contents
ToggleIn this blog post, we will checkout the Subtraction of Two Numbers Program in C.
How to Write Subtraction Program in C?
To create a C program to subtract two numbers, you can follow this algorithm.
Algorithm:
- Start: Begin the process.
- Input two numbers: Prompt the user to input the two numbers.
- Subtract both the numbers: Use the formula num1 – num2 to calculate the subtraction.
- Display answer: Show the calculated answer to the user.
- End: Conclude the process.
Subtraction of Two Numbers Program in C
#include <stdio.h>
int main() {
int number1, number2, result;
// Prompting user for the two numbers
printf("Enter first number: ");
scanf("%d", &number1);
printf("Enter second number: ");
scanf("%d", &number2);
// Subtracting the second number from the first
result = number1 - number2;
// Displaying the result
printf("The subtraction of %d and %d is: %d\n", number1, number2, result);
return 0;
}
Output
Explanation
- Step 1: The program starts by including the standard input-output library
stdio.h
for basic input/output operations. - Step 2: In the
main
function, three integer variables are declared:number1
,number2
, andresult
. - Step 3: The program then prompts the user to enter the first and second numbers, which are read and stored in
number1
andnumber2
respectively usingscanf
. - Step 4: The subtraction operation is performed by subtracting
number2
fromnumber1
, and the result is stored in the variableresult
. - Step 5: Finally, the program prints the result using
printf
.
Time Complexity: The time complexity of this program is O(1).
Space Complexity: The space complexity of the program is also O(1).
If you liked this post you can checkout other C Programs.