# Check if the Sum of Digits of a Number N Divides It

The N number is the special number of integer type that calculates the individual digit of sum. So, this sum will be divisible by its own number.

Let’s take an example of this.

The given integer number, N = 36

The sum of each digit, 3 + 6 = 9

Therefore, 36 is successfully divisible by 9 and it verifies for N divides it.

### Algorithm

The following steps are-

Step 1: We will start the program by mentioning the header file iostream.

Step 2: Common Matches:-

• Then used the function definition digit_sum() that accepts the parameter a variable n of type integer to work on the operation of sum of digits.[Example 1]

• In Example 2, the logical part is same only change is the name of the function definition i.e. sum_of_digits.[Example 2].

Step 3: The function initializes the variable sum to 0 which keeps track of sum of digits.

Step 4: The program uses while loop where it set the condition as n > 0. If the n value is greater than 0 then it will move forward to inside the loop. Then calculates the digit sum verification using the operators- % and /=.

Step 5: Then function simply returns the variable sum that has the calculation of an individual digit of the sum.

Step 6: Then it will use another function definition check_divisibility_of_digit() that accepts the parameter as a variable n and returns the function by taking function reference i.e. digit_sum() with some operators.[Example 1]

Step 7: Now start the main function, and initialize the variable N to set the integer values.

Step 8:

• Next, it calls the function using an if-else statement to check whether the given number of digits of sum is divisible to itself or not. [Example 1]

• Next, it will use the calling function sum_of_digits() in the variable sum. Then the sum will use in the condition if-else statement to check whether the digit of sum is divisible to itself or not.[Example 2]

## Example

The program uses a while loop to calculate the sum of digits whereas the if-else statement will be used to set the condition using the calling function. So this way it will check the sum of digits of a number N divides it.

#include <iostream>
using namespace std;
// This function is used to calculate the sum of the digit
int digit_sum(int n) {
int sum = 0;
while (n > 0) {
sum += n % 10;
n /= 10;
}
return sum;
}
// taking function reference in the return statement
bool check_divisibility_of_digit(int n) {
return n % digit_sum(n) == 0;
}
// Start the main function
int main() {
int n = 48;
if (check_divisibility_of_digit(n)) {
cout << "The given number is divisible by its sum" << endl;
} else {
cout << "The given number not divisible by its sum" << endl;
}
return 0;
}


### Output

 The given number is divisible by its sum


## Example

In the following example, we will use the simple while loop which set the operation for the calculation of the digit sum. In the main function, it uses the calling function and if-else statement to set the specific condition and operation to get the result.

#include <iostream>
using namespace std;
// recursive function
int sum_of_digits(int n) {
int sum = 0;
while (n > 0) {
int digit = n % 10;
sum += digit;
n /= 10;
}
return sum;
}
// Start the main function
int main() {
int n = 42;
// Calling function
int sum = sum_of_digits(n);
if (n % sum == 0)
{
cout << "The given input of sum is satisfied for division." << endl;
}
else
{
cout << "The given input of sum is not satisfied for division." << endl;
}
return 0;
}



### Output

 The given input of sum is satisfied for division.


## Conclusion

The sum of digits is those numbers whose addition is divisible by itself. The program uses a simple while loop to iterate over the input number N to find the sum. After that, it uses some operators in the main functions such as modulo(%) and equal to(==) to get the specific operation over an if-else statement.

Updated on: 17-Aug-2023

39 Views 