In this example, two integers are taken as an input from the user and are added with each other. The result is then displayed to the user.
#include <stdio.h> int main() { int num1, num2, sum; printf("Please enter the number 1: "); scanf("%d", &num1); // Value of number one is stored in num1 printf("Please enter the number 2: "); scanf("%d", &num2); // Value of number two is stored in num2 sum = num1 + num2; // Addition of two numbers printf("Sum: %d", sum); return 0; }
First of all, the stdio.h library will be included in the program which is resposible for taking input and displaying results. Then two numbers will be taken from the user and will be stored in num1 and num2 variables respectively by using scanf.
int num1, num2, sum; printf("Please enter the number 1: "); scanf("%d", &num1); // Value of number one is stored in num1 printf("Please enter the number 2: "); scanf("%d", &num2); // Value of number two is stored in num2
The sum variable will be used to store the result of addition of two variables num1 and num2 in it. Finally, the result stored in sum will be displayed by using the printf statement.
sum = num1 + num2; // Addition of two numbers printf("Sum: %d", sum);
Output
Please enter the number 1: 7 Please enter the number 2: 4 Sum: 11