Most probably, you studied the addition of numbers in childhood (one plus one equals two, right?). Now, you can easily add two numbers manually, but how about using a computer to do that? We will instruct the computer to add the numbers. How the computer does it, we will not discuss, i.e., how the microprocessor adds two numbers.

Suppose you are playing a game with your friend of adding two numbers. You speak two numbers (input). Your friend sums them up and announces the result (output). You do it repeatedly till both of you decide to do something more interesting. You can see that a computer doesn't get bored or tired like humans do. It will continue to do what it's instructed.

To make lemonade, we need lemon, sugar, and water (you may add other ingredients according to your taste). Similarly, to add two numbers, we need them. The numbers to be added is the input for our program, and the result is the output.

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.

## Addition program in C

int main()

{

int x, y, z;

printf("Enter two numbers to add\n");

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

z = x + y;

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

return 0;

}

Output of the program:

Download Add numbers program.

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

## Overflow in addition

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).

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:

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.

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

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

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:

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.

long addition(long, long);

int main()

{

long first, second, sum;

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

sum = addition(first, second);

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

return 0;

}

long addition(long a, long b)

{

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.

int main()

{

int x, y, sum, c;

printf("Enter two numbers to add\n");

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

sum = x;

for (c = 1; c <= y; c++)

sum = sum + 1;

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

return 0;

}