C Program to Add Two Numbers

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

#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;
}
Simple Program to add Two numbers Output

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.

Comments are closed.