×

Add two numbers represented by two arrays in C++

The array stores a number in such a way that each digit of the number is represented by an array element. As an example,

The array number 147 is 1,4,7.

To add to such numbers, we will first add the number at the least significant digit and then propagate the carry if the sum is greater than 10. Following that, we will proceed to the next consecutive digits of the array, repeating the procedure and calculating the sum.

Let's say you want to add two numbers.

a = {1,2,3,4}

b = {4,3,2,6}

Now the numbers are a=1234 and b=4326. Therefore, sum of 1234 and 4326 gives 5560 as the output.

5560 is the output.

Explanation: We will add the most least significant digit of the number, i.e., 4+6 = 10, to propagate a carry, and then 3+2+1 = 6 will be the next sum. This makes the total 5560.

Algorithm

To find the sum of any numbers stored in an array. We'll start by seeing if any number has more digits than others. If yes, we will find the sum up to the digits of the smaller number and then add the digits of the larger number. Along with this, we keep on checking a carry number that will keep track of any carry that may occur in the sum and that requires to be forwarded; it will be zero at first and will be reset to zero before each sum iteration. We will find the sum of the numbers one by one, store it in an array, and then print it.

Example 1:

#include <iostream>
using namespace std;
int Sum(int a[], int b[], int s1, int s2){
   int sum[s1];
   int i = s1 - 1, j = s2 - 1, k = s1 - 1;
   int ca = 0, s = 0;
   while (j >= 0) {
      s = a[i] + b[j] + ca;
      sum[k] = (s % 10);
      ca = s / 10;
      k--;
      i--;
      j--;
   }
   while (i >= 0) {
      s = a[i] + ca;
      sum[k] = (s % 10);
      ca = s / 10;
      i--;
      k--;
   }
   for (int i = 0; i <= s1-1; i++) {
      cout<<sum[i];
   }
}
int main(){
   int a[] = { 5, 6, 9 };
   int b[] = { 3, 8 };
   int s1 = sizeof(a) / sizeof(a[0]);
   int s2 = sizeof(b) / sizeof(b[0]);
   cout<<"The sum of the numbers is ";
   if (s1 >= s2)
      Sum(a, b, s1, s2);
   else
      Sum(b, a, s2, s1);
   return 0;
}

Output:

The sum of the two numbers is 607

Example 2:

#include <iostream>
using namespace std;
int Sum(int a[], int b[], int s1, int s2){
   int sum[s1];
   int i = s1 - 1, j = s2 - 1, k = s1 - 1;
   int ca = 0, s = 0;
   while (j >= 0) {
      s = a[i] + b[j] + ca;
      sum[k] = (s % 10);
      ca = s / 10;
      k--;
      i--;
      j--;
   }
   while (i >= 0) {
      s = a[i] + ca;
      sum[k] = (s % 10);
      ca = s / 10;
      i--;
      k--;
   }
   for (int i = 0; i <= s1-1; i++) {
      cout<<sum[i];
   }
}
int main(){
   int a[] = { 1, 2, 3 };
   int b[] = { 3, 2, 1 };
   int s1 = sizeof(a) / sizeof(a[0]);
   int s2 = sizeof(b) / sizeof(b[0]);
   cout<<"The sum of the numbers is ";
   if (s1 >= s2)
      Sum(a, b, s1, s2);
   else
      Sum(b, a, s2, s1);
   return 0;
}

Output:

The sum of the two numbers is 444

Related Topics

Passing a Vector to a function in C++

A pointer is sent to the function when we feed it an array. However, there are two ways to pass a vector: Pass by Value Pass by Reference A copy of a vector...

3 minutes read.

Pointer to Object in C++

What is a pointer? A pointer in C++ is used to point the variable by storing the address of the variable. In C++, to print the address of the variable, we...

4 minutes read.

How to concatenate two strings in C++

In the C++ programming language, the concatenation of two or even more strings is covered in this section. The term "string concatenation" refers to a collection of characters that join two...

4 minutes read.

C++ | C Plus Plus Data type

Data type in every language is very important. Data type means the different kinds of data that are supported by a particular programming language. A computer language’s data types specify the...

15 minutes read.

Palindrome Number Program in C++

A palindrome number is one that is the same when it is reversed. Palindrome numbers include 22, 33, 44, 55, 66, 77, 88, and 99. Algorithm for Palindrome Numbers Get the user's...

4 minutes read.

4 Pillars of OOPs

OOPs OOPS means Object Oriented Programming System Structure. Object oriented Programming is defined as an approach that provides a way of modularizing programs by creating partitioned memory area for both data...

11 minutes read.

C++ Program to Implement Merge Sort

C++ Program to Implement Merge Sort The technique of merge sort is based on the strategy of divide and conquer. We divide the set of while data into smaller bits, arranged...

3 minutes read.

Virtual Function Vs Pure Virtual Function

Virtual activity is a member function defined in the foundation phase that can be redefined by acquired classes. Let's have a look at an example: #include <iostream>   #include <bits/stdc++.h> #include <stdlib> using namespace std;   class Base   {    ...

5 minutes read.

Constructor Vs Destructor in C++

C++ Constructor A function Object () { [native code] } is a member function that shares the same name as the class. It is called automatically whenever a class object is...

3 minutes read.

Remove duplicates from sorted array in C++

Remove duplicates from the sorted array in C++. This same process, due to a sorted array, is to erase the redundant components from the array. Examples: Input  : arr[] = {2, 2, 2,...

4 minutes read.

Floating Point Operations and Associativity in C, C++ and Java

In this tutorial, we are going to compare Floating-point operations and the concept of associativity. Before we apply the concept of associativity in the floating-point operations in all three programming...

3 minutes read.

How to call a void function in C++

Generally, any function has two types: 1. Void function: It doesn't return any value. 2. Non-void function: It returns some value. Program to call a void function in C++ #include <iostream> using namespace std;  void check() {  ...

2 minutes read.

Factorial of a Number in C++ using while Loop

What is a factorial? The factorial of a number is the product of all the positive numbers less than or equal to n, indicated by n! According to the standard for an...

6 minutes read.

C++ Virtual Function

A virtual function is such function which is declared inside the base class and redefined by the derive class. C++ uses a virtual keyword to make a function as a virtual function. The virtual...

1 minute read.

How to Reverse a String in C++ using For Loop

For Loop: We may loop through a certain section of C++ code repeatedly using the for loop. A for loop is carried out if the test expression yields a true result. The...

4 minutes read.

C++ if-else

C++ if else Control Statement if else control statement in C++ is used to control the program flow in a two-way direction. When condition returns a true value, then the program executes if condition block otherwise...

1 minute read.

std::min in C++

std::min in C++ std::min is specified in the program code, used to calculate the lowest amount that has been transferred. When there's more of someone who returns first of them. It's used...

2 minutes read.

Observer Design Pattern in C++

The Observer design pattern is a behavioural design pattern that allows an object (known as the subject) to notify other objects (known as observers) when its state changes. This is...

3 minutes read.

C++ Pipe Tutorial

A pipe is a mechanism for inter-process communication (IPC) in a Unix-like operating system. It allows two or more processes to communicate with each other by sending and receiving data...

3 minutes read.

Differences between Local and Global Variable

Define Global Variable Global variables are those that may be accessible worldwide across a programme and are defined outside of any functions or blocks. It may be accessed by any function in...

3 minutes read.