---
# System prepended metadata

title: 'Adding Big Numbers [4 kyu]'
tags: [C++, CodeWars]

---

# Adding Big Numbers [4 kyu]

[Adding Big Numbers](https://www.codewars.com/kata/525f4206b73515bffb000b21)
4 kyu

## Solution
```cpp=
/**
*** Author: R-CO
*** E-Mail: daniel1820kobe@gmail.com
*** Date: 2020-08-04
**/
#include <algorithm>
#include <string>

std::string add(const std::string& a, const std::string& b) {
  std::string output;
  int carry = 0;
  auto a_itor = a.rbegin();
  auto b_itor = b.rbegin();
  while (a_itor != a.rend() && b_itor != b.rend()) {
    int sum = (*a_itor++ - '0') + (*b_itor++ - '0') + carry;
    carry = (sum >= 10) ? 1 : 0;
    sum %= 10;
    output += static_cast<char>(sum + '0');
  }
  
  while (a_itor != a.rend()) {
    int sum = (*a_itor++ - '0') + carry;
    carry = (sum >= 10) ? 1 : 0;
    sum %= 10;
    output += static_cast<char>(sum + '0');
  }
  
  while (b_itor != b.rend()) {
    int sum = (*b_itor++ - '0') + carry;
    carry = (sum >= 10) ? 1 : 0;
    sum %= 10;
    output += static_cast<char>(sum + '0');
  }
  
  if (carry == 1) {
    output += '1';
    carry = 0;
  }
  
  std::reverse(output.begin(), output.end());
  
  return output;
}

```

## Result

PASS


###### tags: `CodeWars` `C++`
