
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
C++ Program to Sum the Digits of a Given Number
The task is to write C++ programs that calculates the sum of the digits of a given number. For example, if the number is 453, the sum of its digits would be 4 + 5 + 3 = 12.
To solve this, we will extract each digit from the number and add them together. We will use basic programming concepts such as loops and arithmetic operations to achieve this.
In this article, we will show you how to write a C++ program that takes a number as input and sums the digits of that number.
Approaches for Summing Digits
There are a few different ways to solve this problem. Below are the commonly used approaches.
Using a Loop and Modulus Operation
In this approach, we will use a loop to extract each digit of the number. The modulus operation will allow us to get the last digit of the number. We will add each digit to a sum variable until the number becomes 0.
Example
In this example, we use the modulus operator(%) to get the last digit and integer division(/) to remove the last digit in each iteration of the loop. The sum of all the digits is then calculated and displayed.
#include <iostream> using namespace std; int main() { int number, sum = 0; cout << "Enter a number: "; cin >> number; while (number != 0) { sum += number % 10; // Extract last digit and add to sum number /= 10; // Remove last digit } cout << "Sum of digits: " << sum << endl; return 0; }
For the input 4567, the digits are 4, 5, 6, and 7. We'll extract and add each digit in a loop until all of them are processed. Here's the output:
Enter a number: 4567 Sum of digits: 22
Time Complexity: O(d), where d is the number of digits. In each iteration, we extract one digit and divide by 10.
Space Complexity: O(1), since we only use a constant amount of space for variables(number and sum).
Using Recursion
Recursion is a technique where the function calls itself with a smaller problem. In this case, we use recursion to break the problem into smaller parts by removing the last digit from the number, then passing the remaining number to the function while adding the removed digit to the sum.
Example
In this example, we recursively divide the number by 10 to remove the last digit and add it to the sum. The process continues until all digits are processed, as shown in the code below.
#include <iostream> using namespace std; int sumDigits(int number) { if (number == 0) return 0; // Base case: no digits left return (number % 10) + sumDigits(number / 10); // Add last digit and recurse } int main() { int number; cout << "Enter a number: "; cin >> number; cout << "Sum of digits: " << sumDigits(number) << endl; return 0; }
Let's say the input is 123. We first extract the last digit, 3, and then call the function with 12. This continues until no digits are left. Here's the output:
Enter a number: 123 Sum of digits: 6
Time Complexity: O(d), where d is the number of digits in the number. Each recursive call processes one digit.
Space Complexity: O(d), as the recursion stack will grow with each call until all digits are processed.
Converting the Number to a String
In this approach, we convert the number into a string to easily access each digit as a character. Then, we convert each character back into an integer and add them together.
Example
Here, we first convert the number into a string. Then, we iterate through each character in the string, converting it back to its integer value by subtracting the character '0'. This continues for all digits until the sum is calculated.
#include <iostream> #include <string> using namespace std; int main() { int number, sum = 0; cout << "Enter a number: "; cin >> number; string numStr = to_string(number); // Convert number to string for (char digit : numStr) { sum += digit - '0'; // Convert each character back to integer } cout << "Sum of digits: " << sum << endl; return 0; }
For the input 1234, the string representation would be "1234". We then iterate through each character and sum the digits. Below is the output:
Enter a number: 1234 Sum of digits: 10
Time Complexity: O(d), where d is the number of digits in the number. We iterate over each character in the string.
Space Complexity: O(d), as we store the string representation of the number.
Conclusion
In this article, we looked at three ways to sum the digits of a number in C++: using a loop, recursion, and string conversion. The loop is the fastest, recursion is simple but uses more space, and string conversion is easy but takes extra space. All methods have a time complexity of O(d) but use different amounts of space. Pick the one that works best for you.