C

C Program to Find Sum of Two Numbers

In this tutorial, we are going to see how to write a C program to find the sum of two numbers.

To add two numbers, the user is first asked to type two numbers, then the input is scanned using the scanf() function and stored in “a” and “b” variables. Then, the “a” and “b” variables are added using the + operator and the result is stored in the “sum” variable.
 

C Program to Find Sum of Two Numbers
#include<stdio.h>
 
int main()
{
   int a, b, sum;
   
   printf("Enter two numbers to sum up: \n");
   scanf("%d%d", &a, &b);
   
   sum = a + b;
   
   printf("The sum of the two numbers is = %d\n", sum);
   
   return 0;
}

Output:

Enter two numbers to sum up: 
1
2
The sum of the two numbers is = 3

 

 

C Program to Find Sum of Two Numbers using Function
#include<stdio.h>

//function to calculate the sum of two numbers
int sum(int a, int b)
{
   int res;
 
   res = a + b;
 
   return res;
}

main()
{
   int a, b, res;

   printf("Enter two numbers to sum up: \n");
   scanf("%d%d", &a, &b);

   res = sum(a, b); //call the sum function

   printf("The sum of the two numbers is = %d\n", res);

   return 0;
}

Output:

Enter two numbers to sum up: 
1
2
The sum of the two numbers is = 3

 

mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *