Write a simple C program to add two integer numbers and print the addition or sum output. In this programming language, there is an arithmetic + operator. We can use this operator in between the two or more values to find the sum of them.
Simple C Program to add Two numbers
In this simple program of adding two numbers examples, First, we declared three integer values called number1, number2, and sum. The next two lines of C program code invite the user to enter two integer numbers. The next scanf statement will assign the user entered values to variables that we already declared, and they are number1 and number2.
Next line, we applied Arithmetic Operators + to sum number1 and number2 and assigned that total to the sum. Then the printf will display the sum of two numbers variable as an output (22 + 44 = 66). Refer to the Arithmetic Operators in C.
#include <stdio.h>
int main()
{
int number1, number2, sum;
printf(" Enter two integer values \n ");
scanf("%d %d", &number1, &number2);
sum = number1 + number2;
printf(" Sum of the two integer values is %d", sum);
return 0;
}

This C program to add two numbers performed well while working with positive integers and it returns the sum. What about the combination of positive and negative integers? Let us see.
The program below permits the user to enter two positive or negative integer values. Then, those two values are added, and the result is assigned to the variable sum.
Enter two integer values
8
-6
Sum of the two integer values is 2
As you can see from the above output, this addition of two numbers worked excellently with negative numbers, too.
Addition of two numbers without using a third variable
In the following example, we use the C Assignment operators. Here, we don’t need a third variable to store the addition of two numbers. Instead, it performs the addition and assigns the result to the first variable (number1).
#include <stdio.h>
int main()
{
int number1 = 100;
int number2 = 200;
number1 += number2;
printf("%d\n", number1);
}
300
C Program to add two numbers using functions
In the program below, we declare a UDF that accepts two integer values to perform addition and returns the sum of the two numbers. Within the main program, we allow the user to enter two integer values and pass them to a function to add the two user-given numbers. Refer to the C function article.
#include <stdio.h>
int add(int x, int y)
{
return x + y;
}
int main()
{
int a, b;
printf("Enter two Numbers: ");
scanf("%d %d", &a, &b);
printf("The sum of the two numbers = %d\n", add(a, b));
return 0;
}
Enter two Numbers: 15 20
The sum of the two numbers = 35
C Program to add two numbers without using an operator
Generally, we use the arithmetic + operator to add two numbers. However, we can use the bitwise operators to add two numbers. Please refer to the C Bitwise Operators and the While loop in C articles.
The binary number of 8 = 1000 and binary value of 5 = 0101
While loop Iteration 1:
Total = 1101
a & b = 0000
b = (a & b) << 1 = 0 (left-shifting 0 to n number of places returns 0).
a = total = 1101 = 13.
While loop Iteration 2: The condition (b != 0) fails, and the compiler terminates the loop.
#include <stdio.h>
int main()
{
int a, b, total;
printf("Enter two Numbers: ");
scanf("%d %d", &a, &b);
while (b != 0)
{
total = a ^ b;
b = (a & b) << 1;
a = total;
}
printf("The sum of two numbers = %d\n", a);
return 0;
}
Enter two Numbers: 8 5
The sum of two numbers = 13
Addition of two numbers using macros
In the example below, we defined macros that accept two arguments and perform the addition.
#include <stdio.h>
#define Addition(x, y) ((x) +(y))
int main()
{
int a = 50;
int b = 200;
printf("Sum = %d\n", Addition(a, b));
}
250
Also Read
Comments are closed.