Subtract Two Numbers in C programming

Subtract Two Numbers in C

Program to Subtract Two Numbers in C

Here is a simple C program that subtracts one number from another and displays the result:

#include <stdio.h>

int main() {
    // Declare variables to hold the two numbers and the result
    int num1, num2, difference;

    // Prompt the user to enter the first number
    printf("Enter the first number: ");
    scanf("%d", &num1);

    // Prompt the user to enter the second number
    printf("Enter the second number: ");
    scanf("%d", &num2);

    // Calculate the difference between the two numbers
    difference = num1 - num2;

    // Display the result
    printf("The difference between %d and %d is %d\\n", num1, num2, difference);

    return 0;
}

This program performs the following steps:

  • Include the Standard Library: #include <stdio.h>
  • Declare Variables: int num1, num2, difference;
  • Input Numbers: Use printf to prompt and scanf to read input.
  • Calculate and Display Result: Subtract the numbers and use printf to display the difference.
Note:
Ensure that the input values are correctly entered and matched with the format specifiers to avoid input errors.

Comments

Popular Posts