C Program to Add Two Integers

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

Overview

Adding two integers is a common task, and writing a C program to achieve this lays the foundation for more complex tasks. Such a program enhances your grasp of C's syntax and logic. We have plenty of methods of adding two numbers; some of them are by using the + operator, a function, a user-defined function, the technique of recursion, etc. Some of the ways of adding two integers are: using addition operator (+), using functions, using user-defined functions, using recursion, etc.

C Program to Add Two Numbers Using the Addition(+) Operator

In programming, performing basic arithmetic operations is the cornerstone of problem-solving. Adding two numbers might seem elementary, but it forms the foundation for more complex calculations. In this section, we delve into a C program that demonstrates how to add two numbers using the addition operator (+), accompanied by a comprehensive complexity analysis and detailed explanation.

Code

Output:

Explanation:

The program includes the standard I/O library for input and output functions. Two integer variables, num1 and num2, are declared to store the user's input. The printf function prompts the user to enter two numbers, which are then captured using the scanf function.

The magic happens with the line int sum = num1 + num2;. Here, the + operator adds the two numbers, storing the result in the sum variable. Finally, the result is printed using another printf statement.

Complexity Analysis:

The complexity of this program is minimal. Input operations (scanf) have a time complexity of O(1), involving a fixed number of input variables. The addition operation is constant time (O(1)) as well, regardless of the magnitude of the numbers. Thus, the overall time complexity is O(1).

The program uses a constant amount of memory to store variables irrespective of the input values. Therefore, the space complexity is also O(1).

C Program to Add Two Numbers Using Functions

This section delves into a simple yet illustrative C program that adds two numbers using functions. With a focus on clarity and comprehensibility, this piece offers the code, an analysis of its complexity, and an explanation of the underlying concepts.

Code:

Output:

Explanation:

The code begins by including the necessary standard input/output library using the #include <stdio.h> directive. The add function takes two integer parameters and returns their sum. In the main function, user input is acquired using scanf, and the add function is called with the provided numbers. The result is then displayed using printf.

Complexity Analysis:

The complexity of this program is straightforward. Reading input and displaying output each take O(1) time. The addition operation within the add function is also O(1). Therefore, the overall time complexity is O(1), making this algorithm highly efficient for adding two numbers.

C Program to Add Two Numbers Using a User-defined Function

Adding two numbers might sound simple, but it lays the foundation for more complex computations. This section delves into a C program that adds two numbers using user-defined functions. We'll walk through the code, analyze its complexity, and explain it comprehensively.

Code:

Output:

Explanation:

  • We begin by including the necessary header file stdio.h for input/output operations.
  • The add function takes two integer parameters and returns their sum.
  • Inside the main function, we declare variables num1 and num2 to store the user's input.
  • The user is prompted to enter two numbers using printf and scanf.
  • We call the add function, pass the input numbers, and store the result in the sum variable.
  • The sum is displayed using another printf statement on the screen.

Complexity Analysis:

The code's complexity is primarily determined by the arithmetic operations and the function calls. Both are constant time operations (O(1)), ensuring efficient execution even for large numbers. The scanf operation contributes negligibly to the complexity.

C Program to Add Two Numbers Using Recursion

Recursion is a fascinating technique that involves solving a problem by breaking it down into smaller instances of the same problem. This section dives into a common programming scenario – adding two numbers using recursion. We'll explore the C program for this task, delve into its complexity analysis, and provide a step-by-step explanation of the process.

Code:

Output:

Complexity Analysis:

The complexity of this recursive addition lies in the number of recursive calls required. The addition operation is essentially transformed into bitwise XOR and AND operations. In each recursion, the AND operation calculates the carry, which is then shifted to the left and added to the XOR result. The recursion continues until there is no carry left. As a result, the time complexity is O(log n), where n is the larger of the two input numbers.

Explanation:

  • The program starts by including the necessary header file and defining the addNumbers function that takes two integers as parameters.
  • Within the addNumbers function, the base case checks if the second number (b) is 0. If true, it returns the first number (a), terminating the recursion.
  • If the base case is unmet, the function calculates the sum using bitwise XOR (^) and carries using bitwise AND (&) operations.
  • The carry is then shifted left by one position using << 1.
  • The function calls itself recursively with the modified values of a (XOR result) and b (shifted carry).
  • In the main function, the user is prompted to input two numbers.
  • The addNumbers function is called with the input numbers, and the result is printed as the sum.

Conclusion

  • Adding two integers is a common task, and writing a C program to achieve this lays the foundation for more complex tasks.
  • Such a program enhances your grasp of C's syntax and logic.
  • We have plenty of methods of adding two numbers; some of them are by using the + operator, a function, a user-defined function, the technique of recursion, etc.
  • The time complexity of recursion approach is O(log n), where n is the larger of the two input numbers.