Add Two Numbers in C programming

Add Two Numbers in C

Add Two Numbers in C programming

Here is a simple C program that adds two numbers and displays the result:

#include <stdio.h>

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

    // 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 sum of the two numbers
    sum = num1 + num2;

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

    return 0;
}

This program performs the following steps:

  • Include the Standard Library: #include <stdio.h>
  • Declare Variables: int num1, num2, sum;
  • Input Numbers: Use printf to prompt and scanf to read input.
  • Calculate and Display Result: Add the numbers and use printf to display the sum.
Note:
Ensure to use the correct format specifiers with scanf and printf to match the data types of the variables.

Comments

Popular Posts