What is the C Program to Add Two Numbers?
The question "What is the C Program to Add Two Numbers?" refers to a request for a simple C language program that prompts the user to input two numbers, adds them together, and then displays the result.
The program typically involves:
 Declaring variables to store the numbers and the sum.
 Prompting the user to input the two numbers.
 Reading the input values.
 Adding the numbers together.
 Displaying the sum to the user.
Approach 1: Addition of two numbers in C
Since C language has two numeric data types, hence to find the addition of two numbers in C, we have to declare the numbers and sum variable as an int in the main function, take the input, find the sum and display the output.
The steps of implementation are:

Take the input of two numbers in variables num1 and num2.

Store the sum of num1 and num2 in the variable sum.
 Print the value of the variable sum
Implementation in C
C
// A program for the addition of two numbers in C
#include<stdio.h>
int main(){
//declare num1 and num2 to hold the given numbers
int num1, num2;
//Taking the input
scanf("%d %d", &num1, &num2);
//Calculating the sum of num1 and num2
int sum = num1 + num2;
//Printing the addition of two numbers in C
printf("The sum of the given numbers is: %d", sum);
return 0;
}
Input
45
25
Output
The sum of the given numbers is: 70
Complexities
Time complexity
O(1)
Reason: The complexity of the problem addition of two numbers in C is O(1) because it doesn't depend on the input values.
Space complexity
O(1)
Reason: The space is only taken to store the two variables and their sum hence the space complexity for a program to find the addition of two numbers in C is O(1).
Also check out Addition of Two Numbers in Java here.
Approach 2: Addition of two numbers in C Using a Function
Apart from writing the code in the main function, we can also use a function implementation to sum two numbers and then return the result and use that in the main function. Now we will discuss a program to find the addition of two numbers in C using a function.
The steps of implementation are :

Take input of two numbers in num1 and num2.

Pass num1 and num2 as arguments of userdefined sum function and store the return value in ans variable

In the sum function, return the sum of num1 and num2.
 Print the value of ans
You can also read about dynamic array in c
Implementation in C
C
//program for the addition of two numbers in C using a function
#include<stdio.h>
//function to find the addition of two numbers in C
int sum(int a, int b){
return a+b;
}
int main(){
//declare num1 and num2 to hold the given numbers
int num1, num2;
//Taking the input
scanf("%d %d", &num1, &num2);
//Calculating the sum and storing in ans
int ans = sum(num1,num2);
//Printing sum
printf("The sum of the given numbers is: %d", ans);
return 0;
}
Input
45
25
Output
The sum of the given numbers is: 70
Complexities
Time complexity
O(1)
Reason: The complexity of the problem is O(1) because it doesn't depend on the input values.
Space complexity
O(1)
Reason: The space is only taken to store the two variables and their sum.
Frequently Asked Questions
How to add two numbers in C program?
In C, to add two numbers, you'd declare variables to hold the numbers and their sum, then use the addition operator (`+`) to add them. Example: `int sum = num1 + num2;`.
How to add n numbers in C?
In C, to add n numbers, you typically use a loop. Initialize a variable to hold the sum, then iterate through the numbers, adding each to the sum. For example:
int sum = 0;
for(int i = 0; i < n; i++) {
sum += numbers[i];
}
How to add variables in C?
In C, adding variables is done using the addition operator (+). You can store the result in another variable. For example:
int a = 5, b = 10;
int sum = a + b; // sum will now hold the value 15
What is the sum += in C?
sum += in C is a shorthand notation for incrementing the value of a variable by adding another value to it. It's equivalent to sum = sum + value.
What is an example of the addition of two numbers?
An example of adding two numbers in C: int sum = num1 + num2;, where num1 and num2 are variables containing the numbers to be added.
Conclusion
In this article, we explored how to calculate the sum of two numbers in C. Additionally, we delved into enhancing code reusability by employing a function to perform the addition in a C program.
Are you planning to ace the interviews of reputed productbased companies like Amazon, Google, Microsoft, and more?
Attempt our Online Mock Test Series on Coding Ninjas Studio now!
Recommended Readings: