
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Sum of two numbers where one number is represented as array of digits in C++
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.
Let’s take an example to understand the problem,
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.
Example
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]; }
Output
The sum of two numbers where one number is represented as array of digits is 196398
- Related Articles
- 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++
- Print prime numbers with prime sum of digits in an array
- The sum of two numbers is 95. If one of the numbers exceeds other number by15 then find the numbers?
- Count numbers which can be represented as sum of same parity primes in C++
- Program to add two numbers represented as strings in Python
- The sum of two numbers is $frac{−4}{3}$. If one of the number is $-5$, find the other number.
- One number is greater than the other by 3 the sum of two numbers is 23 find the numbers
- Program to find sum of digits until it is one digit number in Python
- The sum of digits of a two-digit number is 13. If the number is subtracted from the one obtained by interchanging the digits, the result is 45. What is the number?
- The sum of two numbers is 8. If one of them is $- frac{5}{6}$ then the other number is _____.
- Add 1 to number represented as array (Recursive Approach)?
- The sum of the squares of two numbers is 233 and one of the numbers is 3 less than twice the other number. Find the numbers.

Advertisements