# C Code To Add Two Numbers

This tutorial will teach you how to write a C program that adds two numbers. This is a simple C program in which the user is asked to enter two integers, after which the program takes those inputs, stores them in two separate variables, and displays the sum of these integers.

We will write two programs that will calculate the sum of two integer numbers entered by the user. The user is asked to enter two integer numbers in the first program, and the program then displays the sum of these numbers. In the second C program, we use a user defined function to accomplish the same thing.

## Example 1: Write a program to add two integers.

There are two int variables, num1 and num2, in the following example. These variables are used to store the numbers entered by the user. The scanf() function captures user input. Following the storage of the entered values in num1 and num2, a simple arithmetic operation (num1+num2) is performed to determine the sum of num1 and num2.

After calculating and storing the sum of two numbers in a new variable sum, the program prints the value of sum as an output using the printf() function.

```
#include <stdio.h>
int main() {
int num1, num2, sum;
printf("Enter two integers: ");
//Storing user input into variable num1 & num2
scanf("%d %d", &num1, &num2);
// Adding two input numbers
sum = num1 + num2;
printf("Sum of %d and %d is: %d", num1, num2, sum);
return 0;
}
```

**Output:** The user enters the numbers 25 and 35, and the program prints the sum of these two numbers as 60.

## Example 2: Use function to add two integers.

In this program, we use the user-defined function sum() to find the sum of two numbers, which we call from the main function. Refer to this guide to learn more about function: Examples of C Functions

```
#include <stdio.h>
/* User defined function sum that has two int
* parameters. The function adds these numbers and
* return the result of addition.
*/
int sum(int a, int b){
return a+b;
}
int main()
{
int num1, num2, num3;
printf("Enter first number: ");
scanf("%d", &num1);
printf("Enter second number: ");
scanf("%d", &num2);
//Calling the function
num3 = sum(num1, num2);
printf("Sum of the entered numbers: %d", num3);
return 0;
}
```

**Output:**

Enter first number: 22 Enter second number: 7 Sum of the entered numbers: 29