Problem Solving: Sum of Digits (revisited)
Learn to implement programs using functions in C++.
We'll cover the following
So let’s start with our first problem, which is the sum of digits!
Sum of digits
This program requires three main steps.
-
To get a number from the user.
i. If the number is invalid, i.e., not a 6-digit number
ii. Input should be taken again.
-
To calculate the sum of all its digits.
-
To print the result on the screen.
We’ll perform the validity of the number (step 1 (ii) ) and the calculation of the sum of digits (step 2) through separate functions.
-
The
valid6Digit()
method: This takes an integer as a parameter and returnstrue
if the number is a 6-digit number andfalse
otherwise. -
The
digitsSum()
method: This will take as a parameter an integer (of 6 digits) and return the sum of all its digits.
Look at the following implementation:
// This program will print the sum of six digits #include <iostream> using namespace std; //This function will take a number as input for example 123456 and return 21 int digitsSum(int); //prototype bool valid6Digit(int); // boolean because it will true or false int main() { int number; int result = 0; cout << "enter 6 digit number: "; cin >> number; while(!valid6Digit(number)) // if the number is invalid { // take input again. cout << "Wrong input! Must be a 6 digit number"<<endl; cout << "enter 6 digit number: "; cin >> number; } result = digitsSum(number); // Calculating the digit summation cout << "sum of 6 digit number is: " << result << endl; } int digitsSum(int number) { int sum=0; for(int i=1; i<=6; i++) { sum = sum + number % 10; number = number / 10; } return sum; } bool valid6Digit(int number) { if(number>=100000 && number<=999999) return true; return false; }
Remember to never pass any variable which is just for local use in the function. For instance, passing a sum variable to digitsSum() will lose the abstraction of the function. The programmer who calls the function should never know the internal details of how those functions internally allocate memory to perform a specific task. The end user should only know the arguments they have to send and what the function will return.
For example, this will be considered a bad implementation:
int digitsSum(int number, int sum) { sum=0; for(int i=1; i<=6; i++) { sum = sum + number % 10; number = number / 10; } return sum; } int main() { // the 2nd argument shouldn't be the requirement of the function call digitsSum(123456, 0); }
Multiple numbers digit summation
Now, what if the user wanted to display the sum of several different numbers at different locations? We’ll simply call our function digitsSum()
wherever we need digit summation. Here is a small example.
Instruction: Replace the following code in the above playground to test it.
...
int main()
{
int number;
int sum = 0;
cout << "enter 6 digit number: ";
cin >> number;
sum=digitsSum(number);
cout << "sum of 6 digit number is: " << sum << endl;
/*
.
. after a while
.
*/
cout << "enter 6 digit number: ";
cin >> number;
sum=digitsSum(number);
cout << "sum of 6 digit number is: " << sum << endl;
return 0;
}
...
Exercise: What if the user wanted to find the sum of digits of ten more numbers? Try and edit the code in the editor above for at least four more numbers.
This is exactly why we’ll have to generalize the above program so that once the sum of digits is displayed, the program must ask the user whether they want to find the sum of digits of another number or not. We’ll see how to do that in the next lesson.