C Program to Add Two Numbers
This is one of the simplest programs in C and is often the first step for beginners learning programming logic and syntax.
In this tutorial, you’ll learn how to write a C program to take two numbers as input, add them, and display the result.
Concept Overview
C programming allows us to perform arithmetic operations using basic operators. The '+' operator is used to add two numbers.
Program
#include <stdio.h>
int main() {
int num1, num2, sum;
// Taking input from user
printf("Enter first number: ");
scanf("%d", &num1);
printf("Enter second number: ");
scanf("%d", &num2);
// Calculating sum
sum = num1 + num2;
// Displaying result
printf("Sum = %d\n", sum);
return 0;
}Output
Enter first number: 8 Enter second number: 12 Sum = 20
Explanation
- `#include
` – Includes the standard input-output library for using `printf()` and `scanf()` functions. - `int num1, num2, sum;` – Declares three integer variables.
- `scanf()` – Takes user input for the two numbers.
- `sum = num1 + num2;` – Performs the addition operation.
- `printf()` – Displays the result on the screen.