The sum of digits in C++
Determine the total of a given number's digits. This program involves adding the individual digits in a number and displaying their sum as output. Let us look at what the input and output look like in such kinds of programs.
Input: 264
Output: 12
Logic: 2+6+4 = 12
The sum of a given number's digits using a while loop:
To solve the problem, you need to take the measures listed below:
- Obtain the phone number.
- Create a variable and set its value to 0 to store the sum.
- Until the number is not zero, repeat the next two steps.
- Utilizing the remainder '%' operator, find the number's rightmost digit by dividing it by 10 and adding it to the total.
- Using the '/' operator, divide the quantity by 10 to eliminate the last digit.
- Print the amount or send it back.
Program
#include <iostream>
using namespace std;
class SumOfDigits {
public:
int result(int x) {
int add = 0;
while (x != 0) {
add = add + x % 10;
x = x / 10;
}
return add;
}
};
int main() {
SumOfDigits s;
int x1 = 234;
int x2 = 927;
int x3 = 83402;
cout << s.result(x1) << endl;
cout << s.result(x2) << endl;
cout << s.result(x3) << endl;
return 0;
};
Output
![The sum of digits in C++](https://static.tutorialandexample.com/cpp/the-sum-of-digits-in-cpp1.png)
In a single line, how can one compute?
The function below uses a for loop to calculate the sum in one line, but it has three lines of code instead of one. If we pass the pointer to the value we want to sum, we can make it a one-line function.
Program
The program of the previously mentioned strategy is shown below:
#include <iostream>
using namespace std;
class SumOfDigits {
public:
int addition(int x)
{
int add;
for (add = 0; x > 0; add += x % 10, x /= 10)
;
return add;
}
};
int main()
{
SumOfDigits s;
int x1 = 356;
int x2 = 18;
int x3 = 233;
int x4 = 875;
int x5 = 7648;
cout << s.addition(x1) << endl;
cout << s.addition(x2) << endl;
cout << s.addition(x3) << endl;
cout << s.addition(x4) << endl;
cout << s.addition(x5) << endl;
return 0;
}
Output
![The sum of digits in C++](https://static.tutorialandexample.com/cpp/the-sum-of-digits-in-cpp2.png)
Using Class
We will use Class to write the sum of the digits program in this C++ program:
Program
#include<iostream>
using namespace std;
class digitsum
{
private:
int x, add, rem;
public:
void result();
int FinalResult();
};
void digitsum::result()
{
cout<<"Enter any number that needs to be computed: ";
cin>>x;
}
int digitsum::FinalResult()
{
add=0;
while(x != 0)
{
rem = x % 10;
add = add + rem;
x = x / 10;
}
return add;
}
int main()
{
digitsum d;
int add;
d.result();
add = d.FinalResult();
cout<<"\nThe sum of the digits of this number is "<<add;
cout<<endl;
return 0;
}
Output
![The sum of digits in C++](https://static.tutorialandexample.com/cpp/the-sum-of-digits-in-cpp3.png)
The total sum of a given number's digits using recursion:
To solve the problem, take the steps listed below:
- Obtain the number.
- Take the remaining amount and move on to the next set of digits.
- Utilizing the remainder '%' operator, find the number's rightmost digit by dividing it by 10 and adding it to the total.
- To eliminate the last digit, divide the number by 10 using the '/' operator.
- Verify the base scenario using n = 0.
- Print the amount or send it back.
Program
#include <iostream>
using namespace std;
class recursion {
public:
int digitSum(int num)
{
if (num == 0) {
return 0;
}
return (num % 10) + digitSum(num / 10);
}
};
int main(void)
{
recursion r;
cout << r.digitSum(987) << endl;
cout << r.digitSum(342) << endl;
cout << r.digitSum(416) << endl;
cout << r.digitSum(1947) << endl;
cout << r.digitSum(2023) << endl;
return 0;
}
![The sum of digits in C++](https://static.tutorialandexample.com/cpp/the-sum-of-digits-in-cpp4.png)
The sum of a given number's digits when input is a string:
We cannot treat a number as an integer when its digit count surpasses 1019 because the range of long long int cannot accommodate the supplied number. Thus, given a string as input, execute a loop that increases the sum by that character (in this case, a numeric) from the beginning to the end of the string.
To solve the problem, take the steps listed below:
- Set up a variable sum of zero.
- Start a loop and go from zero to the input string's length.
- By converting each character to its integer value, you can add its importance to the total.
- Return total
Program
The program of the previously mentioned strategy is shown below:
#include <iostream>
using namespace std;
int sum(string s)
{
int total = 0;
for (int n = 0; n < s.length(); n++) {
total = total + s[n] - 48;
}
return total;
}
int main()
{
string s = "98765432101234567890";
cout << sum(s);
return 0;
}
Output
![The sum of digits in C++](https://static.tutorialandexample.com/cpp/the-sum-of-digits-in-cpp5.png)