1.
Introduction
2.
Problem statement
2.1.
Example
3.
What is the C Program to Add Two Numbers?
4.
Approach 1: Addition of two numbers in C
4.1.
Implementation in C
4.2.
C
4.3.
Complexities
5.
Approach 2: Addition of two numbers in C Using a Function
5.1.
Implementation in C
5.2.
C
5.3.
Complexities
6.
6.1.
How to add two numbers in C program?
6.2.
How to add n numbers in C?
6.3.
How to add variables in C?
6.4.
What is the sum += in C?
6.5.
What is an example of the addition of two numbers?
7.
Conclusion
Last Updated: Mar 27, 2024
Easy

# Addition of two numbers in C

Teesha Goyal
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

## Introduction

The C program to Add Two Numbers guides us to sum two integer numbers and add them together, and prints the result as the output.

In programming, it is often required to perform the mathematical addition operations of two or more numbers to solve complex problems. For example, If we are given a = 10 and b = 15, we want to find c = a+b in a program. We may need to write a program for the addition of two numbers in C using a function.

## Problem statement

Take input of two numbers and print the sum of those two numbers. Write a program for the addition of two numbers in C. you can run this code on online compiler.

### Example

Input

``````45
25``````

Output

``70``

Explanation

The sum of 45 and 25 is 70; hence 70 is printed.

Must Read what is storage class in c

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

## 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.
• 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

• 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 user-defined 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

• 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 Cint 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.

### 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 product-based companies like Amazon, Google, Microsoft, and more?

Attempt our Online Mock Test Series on Coding Ninjas Studio now!