Skip to content
C++ Better Explained
Go back
Sum of Digits in C++: Add Up the Digits of a Number

Sum of Digits in C++

Adding up the digits of a number — turning 1234 into 1 + 2 + 3 + 4 = 10 — is a classic beginner problem. It uses the same digit-peeling loop as reversing a number, so it’s a perfect next step for practicing the modulo and division combo.


The Idea: Peel and Add

To sum the digits, you visit each digit from right to left using two operators:

Instead of building a reversed number, you simply add each digit to a running total. Here’s the core loop:

#include <iostream>

int main() {
    int number = 1234;
    int sum = 0;

    while (number != 0) {
        sum += number % 10;   // add the last digit
        number /= 10;         // drop the last digit
    }

    std::cout << "Sum of digits: " << sum << "\n";  // 10
    return 0;
}

Trace it: sum grows 0 -> 4 -> 7 -> 9 -> 10, while number shrinks 1234 -> 123 -> 12 -> 1 -> 0. When number reaches 0, every digit has been added.


Reading the Number from the User

In practice you’ll want the number from input. Handling a possible negative sign keeps it robust — take the absolute value first so the modulo behaves as expected:

#include <iostream>
#include <cstdlib>   // std::abs

int main() {
    int number;
    std::cout << "Enter a number: ";
    std::cin >> number;

    number = std::abs(number);
    int sum = 0;
    while (number != 0) {
        sum += number % 10;
        number /= 10;
    }

    std::cout << "Sum of digits: " << sum << "\n";
    return 0;
}
If you're looking to go deeper with C++, the C++ Better Explained Ebook is perfect for you — whether you're a complete beginner or looking to solidify your understanding. Just $19.

The Recursive Version

The same problem has an elegant recursive solution. The idea: the sum of digits of a number is its last digit plus the sum of digits of everything else. The recursion stops when the number reaches 0:

#include <iostream>

int sumOfDigits(int n) {
    if (n == 0) return 0;                 // base case
    return n % 10 + sumOfDigits(n / 10);  // last digit + the rest
}

int main() {
    std::cout << sumOfDigits(1234) << "\n";  // 10
    return 0;
}

Each call handles one digit and passes the shrunken number down. sumOfDigits(1234) becomes 4 + sumOfDigits(123), which becomes 4 + 3 + sumOfDigits(12), and so on until sumOfDigits(0) returns 0. This mirrors the loop exactly — it’s just expressed as function calls instead of iterations.


Bonus: The Digital Root

If you keep summing the digits until a single digit remains, you get the digital root. For 1234: 1 + 2 + 3 + 4 = 10, then 1 + 0 = 1. You can compute it by looping the digit-sum until the number drops below 10:

#include <iostream>

int main() {
    int number = 1234;
    while (number >= 10) {
        int sum = 0;
        int temp = number;
        while (temp != 0) {
            sum += temp % 10;
            temp /= 10;
        }
        number = sum;    // feed the sum back in
    }
    std::cout << "Digital root: " << number << "\n";  // 1
    return 0;
}

This nests the digit-sum loop inside an outer loop that keeps feeding the result back until one digit is left. It’s a neat demonstration of how a small building block composes into something bigger.



Take Your C++ Further

If you’re looking to go deeper with C++, the C++ Better Explained Ebook is perfect for you — whether you’re a complete beginner or looking to solidify your understanding. Just $19.

👉 Get the C++ Better Explained Ebook — $19

📋

Free Download: The 10 Mistakes Every C++ Beginner Makes

A free 1-page checklist that shows the exact traps that slow down every C++ beginner — so you can avoid them from day one.

🔒 No spam. Unsubscribe anytime.


Share this post on:

Written by

Sahil Bora

Software Engineer. Author and creator of C++ Better Explained.


Previous Post
std::array in C++: A Safer, Modern Alternative to C-Style Arrays
Next Post
Find the Maximum and Minimum in C++ (max, min, max_element)

Keep Learning