Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors

Write a program that asks the user to enter two numbers, and then prints the sum of those two numbers.

C
#include <stdio.h>

int main() {
    int num1, num2, sum;

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

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

    // Calculate the sum
    sum = num1 + num2;

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

    return 0;
}

Explanation:

  • In this program, we declare three variables num1, num2, and sum of type int to hold the user input and the sum.
  • We use printf() to display the prompt messages and scanf() to read the user input.
  • Finally, we calculate the sum by adding num1 and num2, and then print the result using printf().

Output:

Output
Enter the first number: 4
Enter the second number: 8
The sum of 4 and 8 is 12