Problem Solving: Application of the Sum of Digits Function

In this lesson, we’ll see an example of how a function works as a module to solve a bigger problem. Using the sum of digits function, we’ll find out the numbers that are not perfect squares.

Before we do that, let us generalize the sum of digits 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. The program should repeat this until the user chooses not to continue.

This approach can be done on any program where a user wants to perform a task multiple times.

Generalizing the sum of digits program

Let’s generalize our sum of digits program.

// This program will print the sum of six digits
#include <iostream>
using namespace std;

bool valid6Digit(int number);
int digitsSum(int); //prototype

int main()
{
    int result = 0;        
    char choice;
    do 
    {
        int number;
        do
        {   
            // take input again.
            cout << "The 6 digit number: ";
            cin >> number;
            if(!valid6Digit(number))
                cout << "Wrong input! Must be a 6 digit number"<<endl;
        }
        while(!valid6Digit(number)); // if the number is invalid repeat, input.
        result = digitsSum(number);
        cout << "The sum of 6 digit number is: " << result << endl;
        cout << "Press (c)ontinue or (s)top \n";
        cin >> choice;   // 
    }
    while(choice == 'c' || choice == 'C');
 }

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;
}







Calculating the sum of digits for multiple numbers

Exercise: How to avoid multiple function calls using a variable?

In the above program, from line 15–23, we have called the valid6Digit() function twice, once for displaying a wrong input message and another time for repeating the do{ } while() loop. How do we avoid the two function calls by only calling it once, storing the answer in a Boolean variable, and repeating it in the condition part of the while(condition)?

Instruction: Change the above playground. You may look at the hint for help.


Checking for not perfect squares

Now, let us see an application for the above program.

Perfect square numbers are squares of an integer. For example, 9 is a perfect square of 3, 16 is of 4, 25 is of 5, and so on. There are infinite perfect squares. Similarly, 8, 10, 11, and so many other numbers are not perfect squares.

To find whether a number is a perfect square, we have to iterate through all the possibilities and check if an integer whose square is that number exists (we’ll write this algorithm in the next chapter).

A large subset of not perfect square numbers can be figured out using the theorem, as it’s known in discrete mathematics, that a number is certainly not a perfect square if its repeated sum of digits is not equal to 1, 4, 7, or 9.

Let’s take the squares of the first 10 numbers to see what we mean.

1 * 1 = 1     -> sum of digits = 1
2 * 2 = 4     -> sum of digits = 4
3 * 3 = 9     -> sum of digits = 9
4 * 4 = 16    -> sum of digits = 7
5 * 5 = 25    -> sum of digits = 7
6 * 6 = 36    -> sum of digits = 9
7 * 7 = 49    -> sum of digits = 13 -> sum of digits = 4
8 * 8 = 64    -> sum of digits = 10 -> sum of digits = 1
9 * 9 = 81    -> sum of digits = 9
10 * 10 = 100 -> sum of digits = 1

We can see that the sum of the digits of the squared numbers is either equal to 1, 4, 7, or 9. The table below shows the perfect squares of the first 20 numbers. Note that the number 13 has a sum of digits of 4 as well, but it is not a perfect square. Hence, the converse of the above fact is not true, i.e., if a repeated sum of digits of a number is equal to 1, 4, 7 or 9, then the number may or may not be a perfect square.

Squares and Digits Sum of Numbers

Number

Square of the number

Digits sum of the square

Number

Square of the number

Digits sum of the square

1

1

1

11

121

4

2

4

4

12

144

9

3

9

9

13

169

1 + 6 + 9 = 16 → 7

4

16

7

14

196

1+ 9 + 6 = 16 → 1 + 6 = 7

5

25

7

15

225

9

6

36

3 + 6 = 9

16

256

2 + 5 + 6 = 13 → 1 + 3 = 4

7

49

4 + 9 = 13 → 1 + 3 = 4

17

289

2 + 8 + 9 = 19 → 1 + 9 = 10 → 1 + 0 = 1

8

64

10 → 1 + 0 = 1

18

324

9

9

81

8 + 1 = 9

19

361

3 + 6 + 1 = 10 → 1 + 0 = 1 → 1

10

100

1

20

400

4

Here, the sum of the digits is a repeated summation, i.e., if the sum of digits is greater than 9, add the digits again and keep doing it until the sum is a single-digit number. For example, in 388, the sum of digits is 19, whose sum of digits is 10, whose sum of digits is 1 (may or may not be a perfect square), but 386 has a sum of digits of 17, which has a repeated sum of digits of 8. Therefore, it is certainly not a perfect square.

Algorithm

In our program we’ll do the following steps:

  1. We will first take a number from the user.

  2. We will then check if it’s a perfect square or not. If the sum of digits for the entered number is not 1, 4, 7, or 9, the number is not a perfect square.

  3. We will display that the number is not a perfect square based on the fact that a number whose repeated sum of digits is not 1, 4, 7, or 9. The converse is not always true. If the repeated sum of digits is 1, 4, 7, or 9, the number may or may not be a perfect square. In that case, we’ll simply print the square root of the number by calling a sqrt() function defined in the cmath library.

See the code below:

// This program will print the sum of six digits
#include <iostream>
#include <cmath>
using namespace std;

int digitsSum(int); //prototype
int main()
{
    int number;
    int result = 0;
    cout << "The number: ";
    cin >> number;
    result = digitsSum(number);
    
    if(result >= 10)   // this is incomplete
        result = digitsSum(result);

    cout << "sum of digits = " << result << endl; 

    if (result == 1 || result == 4 || result == 7 || result == 9)
    {
        double num = sqrt(number); 
        // sqrt function returns the floating value 
        // num may or may not be an integer
        cout << "The square root of the number is: " << num << endl;      
    }  
    else 
        cout << "The number is not a perfect square. " << endl;    

 }
int digitsSum(int number)
{
    int sum=0;
    while(number!=0)  // we've removed the requirement of 6 digit number
    {
        sum = sum + number % 10;  // getting the last digit
        number = number / 10;     // shrinking the number from the right end
    }
    return sum;
}




Checking if the number is a perfect square

Not only is the above program an example of one of the applications of the sum of digits, but it’s also an example that shows how we can call a function whenever we need it without having to rewrite a chunk of code again and again.

For now, we have used a built-in sqrt() function in line 22 which is defined in the cmath header file in line 3. We’ll learn to implement this function in one of our upcoming lessons as well.

Exercise: Adding repeated summation code

The above code assumes that a maximum of two calls to the digit summation is sufficient. However, it could be possible that the digit summation is again a two-digit number.

Add a condition or loop to make sure that the digit summation should be called repeatedly until the sum of digits is less than 10, and then test the summation for the not perfect square test. If you’re having trouble, look at the highlighted part from line 13 to 16.