You're online now.

Hurray! you are online now.

C Program to Add Two Numbers

The C program to add two numbers takes two integer number value as input from the user, and add them together using the “+” operator, and display the sum on the screen using printf() function.

// Write a C program to sum of two given number from the user.
    int main() {
        int num1, num2, sum;
        printf("Enter two numbers: ");
        scanf("%d %d", &num1, &num2);
        sum = num1 + num2;
        printf("The sum of %d and %d is %d", num1, num2, sum);
        return 0;
  1. The program starts with the inclusion of the stdio.h standard input-output library header file.
  2. The main() function is defined.
  3. Three variables of integer data type ‘num1’, ‘num2’, ‘sum’ are declared.
  4. A message is displayed on the screen using the printf() function to prompt the user to enter two numbers.
  5. The scanf() function is used to take the input from the user, which are stored value in the variable ‘num1’ and ‘num2’.
  6. Calculate the sum of two numbers using ‘+’ operator and stored the value in the variable ‘sum’.
  7. The result (sum) is displayed on the screen using the printf() function along with the original two numbers. The format specifier %d is used to represent the integer values.
  8. The return statement is used to exit the program. Here, 0 is returned to indicate that the program has been successfully terminated.
🖤 0
Buy me coffee ☕



No comments here...