Press "Enter" to skip to content

C Program to Accept Two Numbers and Their Sum

This is a simple program for C programming beginners. This program simply accepts two integer numbers and performs addition operation and finally display the sum result.

#include<stdio.h>
#include<conio.h>
void main()
{
int num1, num2, sum;
printf("Enter the first number: ");
scanf("%d",&num1);
printf("Enter the second number: ");
scanf("%d",&num2);
sum = num1+ num2;
printf("Sum of %d and %d is %d",num1,num2,sum);
getch();
}

Program Explanation:
Program accepts two numbers into variables num1, num2 separately. Sum is another variable with gets the sum of num1 and num2. Finally display the accepted numbers and sum.

You can accept both numbers together, then scanf code will be:
printf(“Enter two numbers: “);
scanf(“%d%d”,&num1,&num2);
All remaining codes are same.

Instead of storing sum result into the variable ‘sum’ you can directly display it on the output printf section.
printf(“Sum of %d and %d is %d”,num1,num2,num1+num2);

Now updated code:

#include<stdio.h>
#include<conio.h>
void main() { int num1, num2; printf("Enter two numbers: "); scanf("%d%d",&num1,&num2); printf("Sum of %d and %d is %d",num1,num2,num1+num2); getch(); }

Sample Output:
Enter the first number: 12
Enter the second number: 8
Sum of 8 and 12 is 20

Sample Screen shot:

Accept two numbers and find sum in C

Be First to Comment

Leave a Reply