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
C
#include <stdio.h>
int main() {
int num1, num2, sum;
printf("Enter first number: ");
scanf("%d", &num1);
printf("Enter second number: ");
scanf("%d", &num2);
sum = num1 + num2;
printf("Sum = %d\n", sum);
return 0;
}
Output
Enter first number: 8 Enter second number: 12 Sum = 20
Explanation
#include <stdio.h>– Includes the standard input-output library for usingprintf()andscanf()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.