11/* *
2- * @file
3- * \brief Program to check if a number is an [Armstrong/Narcissistic
4- * number](https://en.wikipedia.org/wiki/Narcissistic_number) in decimal system.
5- *
6- * \details
7- * Armstrong number or [Narcissistic
8- * number](https://en.wikipedia.org/wiki/Narcissistic_number) is a number that
9- * is the sum of its own digits raised to the power of the number of digits.
10- * @author iamnambiar
11- */
12- #include < cassert>
13- #include < cmath>
14- #include < iostream>
2+ * @file
3+ * @brief Program to check if a number is an [Armstrong/Narcissistic
4+ * number](https://en.wikipedia.org/wiki/Narcissistic_number) in decimal system.
5+ *
6+ * @details
7+ * Armstrong number or [Narcissistic
8+ * number](https://en.wikipedia.org/wiki/Narcissistic_number) is a number that
9+ * is the sum of its own digits raised to the power of the number of digits.
10+ *
11+ * let n be the narcissistic number,
12+ * \f[F_b(n) = \sum_{i=0}^{k-1}d_{i}^{k}\f] for
13+ * \f$ b > 1 F_b : \N \to \N \f$ where
14+ * \f$ k = \lfloor log_b n\rfloor is the number of digits in the number in base \f$b\f$, and
15+ * \f$ d_i = \frac{n mod b^{i+1} - n mod b^{i}}{b^{i}} \f$
16+ *
17+ * @author [Neeraj Cherkara](https://github.com/iamnambiar)
18+ */
19+ #include < cassert> // / for assert
20+ #include < cmath> // / for std::pow
21+ #include < iostream> // / for IO operations
1522
1623/* *
17- * Function to calculate the total number of digits in the number.
24+ * @brief Function to calculate the total number of digits in the number.
1825 * @param num Number
1926 * @return Total number of digits.
2027 */
@@ -28,16 +35,17 @@ int number_of_digits(int num) {
2835}
2936
3037/* *
31- * Function to check whether the number is armstrong number or not.
32- * @param num Number
38+ * @brief Function to check whether the number is armstrong number or not.
39+ * @param number to be checked
3340 * @return `true` if the number is armstrong.
3441 * @return `false` if the number is not armstrong.
3542 */
3643bool is_armstrong (int number) {
37- // If the number is less than 0, then it is not a armstrong number.
44+ // If the number is less than 0, then it is not an armstrong number.
3845 if (number < 0 ) {
3946 return false ;
4047 }
48+
4149 int sum = 0 ;
4250 int temp = number;
4351 // Finding the total number of digits in the number
@@ -46,17 +54,17 @@ bool is_armstrong(int number) {
4654 int rem = temp % 10 ;
4755 // Finding each digit raised to the power total digit and add it to the
4856 // total sum
49- sum = sum + std::pow (rem, total_digits);
57+ sum += static_cast < int >( std::pow (rem, total_digits) );
5058 temp = temp / 10 ;
5159 }
5260 return number == sum;
5361}
5462
5563/* *
56- * Function for testing the is_armstrong() function
57- * with all the test cases.
58- */
59- void test () {
64+ * @brief Self-test implementations
65+ * @returns void
66+ */
67+ static void test () {
6068 // is_armstrong(370) returns true.
6169 assert (is_armstrong (370 ) == true );
6270 // is_armstrong(225) returns false.
@@ -69,12 +77,15 @@ void test() {
6977 assert (is_armstrong (0 ) == true );
7078 // is_armstrong(12) returns false.
7179 assert (is_armstrong (12 ) == false );
80+
81+ std::cout << " All tests have successfully passed!\n " ;
7282}
7383
7484/* *
75- * Main Function
76- */
85+ * @brief Main Function
86+ * @returns 0 on exit
87+ */
7788int main () {
78- test ();
89+ test (); // run self-test implementations
7990 return 0 ;
8091}
0 commit comments