easy
0 views
Subtract Two Numbers
Read two integers from input and output their difference.
Understand the Problem
Problem Statement
The program must accept two numbers and subtract them.
Constraints
- The input will consist of exactly two integers separated by a space
- Each integer will be within the range of standard 32-bit signed integers
- The output should be the result of the first number minus the second number
Examples
Example 1
Input
100 40Output
60Explanation
100 - 40 = 60. The first number (100) minus the second number (40) equals 60.
Example 2
Input
50 200Output
-150Explanation
50 - 200 = -150. When the second number is larger than the first, the result is negative.
Solution
#include <stdio.h>
int main() {
int num1, num2;
scanf("%d %d", &num1, &num2);
printf("%d\n", num1 - num2);
return 0;
}Time:O(1)
Space:O(1)
Approach:
1. Include the standard input/output header file
2. Declare two integer variables to store the input numbers
3. Use scanf to read two integers from standard input
4. Calculate the difference using the subtraction operator
5. Print the result followed by a newline
6. Return 0 to indicate successful execution
Visual Explanation
Loading diagram...