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
C
#include <stdio.h>
int main() {
int num1, num2, difference;
printf("Enter first number: ");
scanf("%d", &num1);
printf("Enter second number: ");
scanf("%d", &num2);
difference = num1 - num2;
printf("Difference = %d\n", difference);
return 0;
}
Output
Enter first number: 15 Enter second number: 7 Difference = 8
Explanation
#include <stdio.h>– Includes the standard input-output library for usingprintf()andscanf()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.