×

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

C++ OOPs Concept

The main goal of C ++ programming is to add the idea of ​​object orientation to the C programming language. Inheritance, data binding, polymorphism and other concepts are part of...

4 minutes read.

Program to convert infix to postfix expression in C++

Parentheses are frequently employed in mathematical formulas to make their interpretation easier to understand. However, with computers, parenthesis in an expression might lengthen the time it takes to find a...

7 minutes read.

C++ Continue in Do-while loop

Continue statement: Inside the loop, the continue statement is used to control the loop. C++ utilizes the continue keyword to implement the continue statement, which transfers the program's flow at the start...

4 minutes read.

Functors in C++

Functors are not a very popular thing among beginner or intermediate-level programmers. But this thing is very useful and helpful. The name functor suggests us some similarities with function. It...

3 minutes read.

Bitwise Operator vs Logical Operator

Bitwise Operator  Bitwise operators perform operations bit by bit on bits.The value is converted to abinary during operations like addition, subtraction, division, and so on. These operations are carried out at the...

3 minutes read.

C++ Program to find largest subarray with 0 sum

Write a program to find the largest subarray that has a sum zero. The array contains positive and negative numbers. Print the length of the max subarray whose sum turns...

4 minutes read.

Palindrome using Do-while loop in C++

What is Palindrome? A palindrome is a word, number, phrase, or other sequence of letters that reads the same backward as forward, such as 101 or MOM. Like other programming languages, C++...

5 minutes read.

C ++ Program: Alphabet Triangle and Number Triangle

Alphabet Triangle and Number Triangle An alphabet triangle is a triangle that typically looks like a pyramid or other triangles like an isosceles triangle, a right-angled triangle consisting of similar or...

4 minutes read.

C++ Dijkstra Algorithm Using the Priority Queue

In this article we will be finding the shortest routes from a source vertex in a graph to all vertices in the graph, given a graph and a source vertex...

5 minutes read.

C++ Inheritance

What do you mean by Inheritance ? The ability to define new classes based on existing classes in order to reuse and organise code is referred to as inheritance. Single inheritance...

7 minutes read.

Malloc() and new in C++

In C ++, malloc () and new are used for the same thing. During runtime, they are used to allocate memory. Malloc () and the new, on the other hand,...

4 minutes read.

Queue in C++

What is Queue? As the name suggests, the queue is the type of data structure that follows the FIFO (First In - First Out) mechanism. In simple words, it is...

4 minutes read.

C++ Goto

In this article, we will discuss the C++ goto statement with its syntax, use, key features, key points, pseudo code, and examples. What is the goto statement in C++? In C++, the...

4 minutes read.

C++ array of Pointers

Array of Pointers: In high-level programming languages like C++, the array's name is its pointer. The name of an array contains an address which is the address of an element. In...

4 minutes read.

Templates in C++ vs Generics in Java

As the title suggests, there is no rivalry or there is no cut comparison between generics and templates in Java and C++, respectively. The main aim of this article is...

4 minutes read.

Use of Inheritance in C++

What is inheritance in c++? Inheritance is fundamental in object-oriented programming (OOP) languages like C++. It allows a programmer to create a new class (called a derived class) that inherits the...

4 minutes read.

C++ First Program

Let's write a simple basic program structure of C++, its compilation and its execution (how it runs). This program is compiled using GCC compiler. Open any editor to write C++ program. #include<iostream>   using namespace std;   int main(){       cout<<"Welcome to C++ program"<<endl;   } Output...

2 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.

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.

Containership in C++

In C++, it is possible to create an object in one class into another class, and that object is a member of another class. This relationship is known as a...

4 minutes read.