- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

In this problem, we are given two numbers, from which one is represented using array of digits. Our task is to create a program that will find the sum of two numbers where one number is represented as array of digits.

Input: n = 213, m[] = {1, 5, 8, } Output: 371 Explanation: 213 + 158 = 371

To solve this problem, we will simply digit by digit from the number which element of the array. It lsb of the number is added to the (n-1)th element of the array. The carry will be propagated for the next sum.

Program to illustrate the working of our solution,

#include <iostream> using namespace std; void addNumbers(int n, int size, int *m){ int carry = 0; int sum = 0; for(int i = size-1; i >= 0; i--){ sum = (n%10) + m[i] + carry; n /= 10; carry = sum/10; m[i] = sum%10; } } int main() { int n= 679; int m[] = {1, 9, 5, 7, 1, 9}; int size = sizeof(m)/sizeof(m[0]); cout<<"The sum of two numbers where one number is represented as array of digits is "; addNumbers(n, size, m); for(int i = 0; i < size; i++) cout<<m[i]; }

The sum of two numbers where one number is represented as array of digits is 196398

- Related Questions & Answers
- Adding one to number represented as array of digits in C++?
- Adding one to number represented as array of digits in C Program?
- Minimum sum of two numbers formed from digits of an array in C++
- Check if a number can be represented as a sum of 2 triangular numbers in C++
- Count numbers which can be represented as sum of same parity primes in C++
- Express an odd number as sum of prime numbers in C++
- Program to add two numbers represented as strings in Python
- Check if a number can be represented as sum of non zero powers of 2 in C++
- Count Triplets such that one of the numbers can be written as sum of the other two in C++
- Multiply Large Numbers represented as Strings in C++
- C program for a number to be expressed as a sum of two prime numbers.
- Sum of two large numbers in C++
- Find smallest number with given number of digits and sum of digits in C++
- Represent a number as a Sum of Maximum Possible Number of Prime Numbers in C++
- Multiply two numbers represented as linked lists into a third list in C++

Advertisements