Simple C Program to add Two numbers

Write a simple C program to add two integer values and print the output?

Simple C Program to add Two numbers

The addition of two numbers in the C program permits the user to enter two integer values. And then, add those two integer numbers and allot the total to variable sum.

/* Simple C Program to add Two numbers */

#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 C Program to add Two numbers 1

In this simple c program to two numbers, First, we declared three integer values called as number1, number2, and sum

int number1, number2, sum;

The following 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.

  printf(" Enter two integer values \n");
  scanf("%d %d", &number1, &number2);

Next line, we applied Arithmetic Operators + to sum number1 and number2 and assigned that total to sum.

sum = number1 + number2;

The following printf statement will display the sum variable as an output (22 + 44 = 66).

printf(" Sum of the two integer values is %d", sum);

This addition of two numbers in c program performed well while adding positive integers, what about adding positive and negative integer? Let us see

 Enter two integer values 
 8
-6
 Sum of the two integer values is 2

It worked excellent with negative numbers too

Comments are closed.