C Program for Subtraction of Two Numbers – Subtraction is a fundamental arithmetic operation that involves finding the difference between two numerical values. In computer programming, subtraction can be efficiently executed using various programming languages, including the popular language, C. In this article, we’ll explore a simple C program that performs subtraction between two numbers.
Table of Contents
Subtraction, as an arithmetic operation, involves taking away one quantity from another to find the difference. In C programming, this operation can be performed using the subtraction operator (-). The program will prompt the user to enter two numbers and then subtract the second number from the first to obtain the result.
C Program for Subtraction
Let’s dive into the C program that demonstrates the subtraction of two numbers:
#include <stdio.h>
void main() {
int num1, num2, result;
// Prompting the user to enter the first number
printf("Enter the first number: ");
scanf("%d", &num1);
// Prompting the user to enter the second number
printf("Enter the second number: ");
scanf("%d", &num2);
// Performing the subtraction operation
result = num1 - num2;
// Displaying the result of subtraction
printf("The result of %d - %d is: %d\n", num1, num2, result);
}
Output
C Program for Subtraction Explanation
- This program starts by including the necessary header file
stdio.h
, which provides input and output functionalities. Then, it defines themain()
function where the actual execution begins. - The program declares three integer variables:
num1
,num2
, andresult
to store the two input numbers and the subtraction result, respectively. - The user is prompted to enter the first number using
printf()
andscanf()
functions, which respectively display the message and read the input from the user. - Similarly, the program prompts for the second number and reads it using
scanf()
. - The subtraction operation
result = num1 - num2;
calculates the difference betweennum1
andnum2
, storing the result in theresult
variable. - Finally, the program prints the result of the subtraction using
printf()
by displaying the equation and the computed result.
Happy Coding