Home / Programs / Program to find sum of two numbers using c programming language.
Programming Example

Program to find sum of two numbers using c programming language.

👁 12,090 Views
💻 Practical Program
📘 Step by Step Learning

In this example, the user is asked to enter two integers. Then, the sum of these two integers is calculated and displayed on the screen.

To understand this example, you should have the knowledge of the following C programming topics:

  • C Data Types
  • C Variables, Constants and Literals
  • C Input Output (I/O)
  • C Programming Operators

Flow chart

C program to add two numbers
Figure:

Program Code

#include"stdio.h"
 void main()
	{
	int num1, num2, sum; 
	printf("Enter two no: ");
 	scanf("%d%d",&num1,&num2);
	sum = num1 + num2;
        printf("sum=%d \n", sum );    
}

Output

Enter two no: 2 3
sum=5
Press any key to continue . . .

Explanation

Algorithm:

  • Step 1: Start
  • Step 2: Declare variable number1, number2 and sum
  • Step 3: Read Variable number1, number2
  • Step 4: Perform operation (sum= number1+ number2)
  • Step 5: Print sum variable
  • Step 7: Stop

In this program, the user is asked to enter two integers. These two integers are stored in variables number1 and number2 respectively.

printf("Enter two integers: ");
scanf("%d %d", &num1, &num2);

Then, these two numbers are added using the + operator, and the result is stored in the sum variable.

sum = num1 + num2;

Finally, the printf() function is used to display the sum of numbers.

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

How to learn from this program

First read the algorithm, then study the program code line by line. After that, compare the code with the output and finally go through the explanation. This approach helps learners understand both the logic and the implementation properly.