#include <iostream>
using namespace std;
int main()
{
int number = 147; //Any number.
int res;
if(number)
res = number % 9 == 0 ? 9 : number % 9 ;
else
res = 0;
//print the result
cout<<res;
return 0;
}

Output

3

Explanation

Yes. This is the complete program for this tricky challenge. Let me explain what is going on under the hood.

There are divisibility rules for every number. For 9, the divisibility rule says, the sum of the digits must be divisible by 9. Now, if the sum is perfectly divisible by 9, then we return 9. Say, 99 for example, 9 + 9 = 18 and 1 + 8 = 9. Hence, the rule follows.

But for non-divisible numbers, we will print the remainder which in case is the sum of the digits, since 9 is the largest single digit number. Applying the modulus 9 on that number, we will get the final remainder which is the sum of the digits. Because, the number 9 is the largest single digit number. So it returns the final sum after applying the modulus.

Now, we are checking if the number is non-zero. If not, check for its divisibility with 9. If it is divisible, pass value 9 otherwise number % 9. If it is 0, then simply print 0.

Do you like this program? Let me know through comments.

Here is the program for this tricky challenge:Output

ExplanationYes. This is the complete program for this tricky challenge. Let me explain what is going on under the hood.

There are divisibility rules for every number. For 9, the divisibility rule says, the sum of the digits must be divisible by 9. Now, if the sum is perfectly divisible by 9, then we return 9. Say, 99 for example,

9 + 9 = 18and1 + 8 = 9. Hence, the rule follows.But for non-divisible numbers, we will print the remainder which in case is the sum of the digits, since 9 is the largest single digit number. Applying the modulus 9 on that number, we will get the final remainder which is the sum of the digits. Because, the number 9 is the largest single digit number. So it returns the final sum after applying the modulus.

Now, we are checking if the number is non-zero. If not, check for its divisibility with 9. If it is divisible, pass value 9 otherwise number % 9. If it is 0, then simply print 0.

need an explanation for this answer? contact us directly to get an explanation for this answerDo you like this program? Let me know through comments.