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.
#include<stdio.h>
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;
}
- The program starts with the inclusion of the stdio.h standard input-output library header file.
- The main() function is defined.
- Three variables of integer data type ‘num1’, ‘num2’, ‘sum’ are declared.
- A message is displayed on the screen using the printf() function to prompt the user to enter two numbers.
- The scanf() function is used to take the input from the user, which are stored value in the variable ‘num1’ and ‘num2’.
- Calculate the sum of two numbers using ‘+’ operator and stored the value in the variable ‘sum’.
- 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.
- The return statement is used to exit the program. Here, 0 is returned to indicate that the program has been successfully terminated.
Comments
Oops!