C Program to Add Two Numbers and Display Sum

Program to add two numbers in C involves declaring variables, reading input numbers, finding their sum and displaying the result.

C Program to add two numbers


#include<stdio.h>
#include<conio.h>

int main()
{
	int a, b, sum; /* Variables Declaration */
	clrscr(); /* Clears the screen */
	printf("Enter the value of a: ");
	scanf("%d", &a); /* Reading First Number */
	printf("Enter the value of b: ");
	scanf("%d", &b); /* Reading Second Number */
	sum = a + b; /* Finding Sum */
	printf("Sum of %d and %d is %d.",a,b,sum);
	getch(); /* Holds the output */
	return 0;
}
--------------------------------------------------
Note: If you are using compiler other than Turbo C 
      then you do not need clrscr(), getch() and 
      #include<conio.h>

Output of the above program :

Enter the value of a: 13 ↲
Enter the value of b: 33 ↲
Sum of 13 and 33 is 46.

Note: ↲ indicates ENTER is pressed.

Program (Adding Two Numbers) Explanation (Step Wise)

  1. In this program, the statement int a, b, sum; creates (or declares) three variables a, b, sum of type integer.

    Variables must be declared first before using them in C program.

    Variables when defined using int can store only integer data i.e. we can store number like 12, -78, 1009 etc but we can not store fractional number like 67.89, -89.99 etc.
  2. After declaring variable, clrscr(); is executed which clears the previous output (if any).
  3. Here, printf("Enter the value of a: "); displays the message Enter the value of a:
  4. When message is printed then scanf("%d", &a); is executed which reads value given by user and then stores it to variable a.
  5. Similarly, Enter value of b: is printed and value given by user is read and stored to variable b.
  6. In this output, first value given by user is 13 which is stored in variable a and second value given by user is 33 which is stored to b.
  7. Now, sum = a + b; is executed which adds a and b and result of addition is assigned to variable sum.

    Operator = is known as Assignment Operator.
  8. Finally, printf("Sum of %d and %d is %d.",a,b,sum); executed which prints Sum of 13 and 33 is 46.

%d used in this program is known as format specifier and they are different for different data types. Think of them as placeholder for printing value of variable while using with printf() and value formatter while using with scanf().

Adding Fractional Numbers


#include<stdio.h>
#include<conio.h>

int main()
{
	float a, b, sum;
	clrscr(); /* Clears the screen */
	printf("Enter the value of a: ");
	scanf("%f", &a);
	printf("Enter the value of b: ");
	scanf("%f", &b);
	sum = a + b;
	printf("Sum of %f and %d is %f.",a,b,sum);
	getch(); /* Holds the output */
	return 0;
}

Output of the above program :

Enter the value of a: 7.5 ↲
Enter the value of b: 8.7 ↲
Sum of 7.500000 and 8.700000 is 16.200000.


Note: ↲ indicates ENTER is pressed.

In the modified program format specifier %f is used because it is the correct format for float data types.