C Program to Subtract Two Numbers
This program demonstrates how to subtract two numbers in C, a basic operation for beginners to learn arithmetic operations and input-output functions.
In this tutorial, you’ll learn how to write a C program to take two numbers as input, subtract the second from the first, and display the result.
Concept Overview
C programming allows arithmetic operations using basic operators. The '-' operator is used to subtract one number from another.
Program
#include <stdio.h>
int main() {
int num1, num2, difference;
// Ask the user to enter two numbers
printf("Enter first number: ");
scanf("%d", &num1);
printf("Enter second number: ");
scanf("%d", &num2);
// Perform subtraction
difference = num1 - num2;
// Display the result
printf("Difference = %d\n", difference);
return 0;
}
Output
Enter first number: 15 Enter second number: 7 Difference = 8
Explanation
- `#include
` – Includes the standard input-output library for using `printf()` and `scanf()` functions. - `int num1, num2, difference;` – Declares three integer variables.
- `scanf()` – Takes user input for the two numbers.
- `difference = num1 - num2;` – Performs the subtraction operation.
- `printf()` – Displays the result on the screen.