# Addition of two numbers in C The addition of two numbers in C language is the arithmetic operation of adding them and printing their sum on the screen. For example, if the input is 5 and 6, the output is 11.

#include <stdio.h>

int main()
{
int x, y, z;

scanf("%d%d", &x, &y);

z = x + y;

printf("Sum of the numbers = %d\n", z);

return 0;
}

Output of the program: Similarly, we can write a C program that performs subtraction, multiplication, and division of two numbers.

In the expression (z = x + y), integer overflow may occur if the sum is greater than the maximum value that the variable z can store. Instead of 9518406073 (1234567891 + 8283838182), the result is 928471481 because of the overflow. The maximum value an integer of 4 bytes can hold is 2,147,483,647.

## Real numbers addition C program

The program can add only integers. What if you want to add numbers with decimals? To do this, we need to use `double` data type (you can also use `float` or `long double` data types).

#include <stdio.h>

int main()
{
double a, b, c;

printf("Enter two numbers\n");
scanf("%lf%lf", &a, &b);

c = a + b;
printf("%lf\n", c);

return 0;
}

Output:

Enter two numbers
3.21
7.47
10.680000

By default, %lf prints six decimal digits. To print up to two decimal digits, use '%.2lf' in the printf function.

#include <stdio.h>

int main()
{
double a, b, c;

printf("Enter two numbers\n");
scanf("%lf%lf", &a, &b);

c = a + b;

printf("%.1lf\n", c);
printf("%.2lf\n", c);
printf("%.3lf\n", c);
printf("%.4lf\n", c);
printf("%.5lf\n", c);
printf("%.6lf\n", c);
printf("%.7lf\n", c);
printf("%.8lf\n", c);

return 0;
} ## Sum of two numbers in C

#include<stdio.h>

int main()
{
int a = 1, b = 2;

/* Storing result of the addition in variable a */

a = a + b;

printf("Sum of a and b = %d\n", a);

return 0;
}

Doing this isn't recommended because the original value of the variable 'a' is lost; if we require it further in the program, then we will not have it.

## C program to add two numbers repeatedly

#include <stdio.h>

int main()
{
int a, b, c;
char ch;

while (1) {
printf("Input two integers\n");
scanf("%d%d", &a, &b);
getchar();

c = a + b;

printf("(%d) + (%d) = (%d)\n", a, b, c);

printf("Do you wish to add more numbers (y/n)\n");
scanf("%c", &ch);

if (ch == 'y' || ch == 'Y')
continue;
else
break;
}

return 0;
}

Output of program:

Input two integers
2 6
(2) + (6) = (8)
Do you wish to add more numbers (y/n)
y
Input two integers
2 -6
(2) + (-6) = (-4)
Do you wish to add more numbers (y/n)
y
Input two integers
-5 3
(-5) + (3) = (-2)
Do you wish to add more numbers (y/n)
y
Input two integers
-5 -6
(-5) + (-6) = (-11)
Do you wish to add more numbers (y/n)
n

## C program for addition of two numbers using a function

We can calculate sum of two integers using a function.

#include<stdio.h>

int main()
{
long first, second, sum;

scanf("%ld%ld", &first, &second);

printf("%ld\n", sum);

return 0;
}

{
long result;

result = a + b;

return result;
}

We are using the long data type as it can handle large numbers. To add numbers that don't fit in in-built data types, use an array, a string, or other suitable data structure.

## C program to add two numbers

Adding a to b (assuming b >= 0) is equivalent to adding one b times to a. For instance, 3 + 5 = 3 + 1 + 1 + 1 + 1 + 1 (adding one five times to 3). Let's implement it through a program.

#include <stdio.h>

int main()
{
int x, y, sum, c;