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
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)
1118 */
12- #include < cassert>
13- #include < cmath>
14- #include < iostream>
19+ #include < cassert> // / for assert
20+ #include < cmath> // / for std::pow
1521
1622/* *
17- * Function to calculate the total number of digits in the number.
18- * @param num Number
19- * @return Total number of digits.
23+ * @brief Function to calculate the total number of digits in the number.
24+ * @param num Number
25+ * @return Total number of digits.
2026 */
2127int number_of_digits (int num) {
2228 int total_digits = 0 ;
@@ -28,16 +34,17 @@ int number_of_digits(int num) {
2834}
2935
3036/* *
31- * Function to check whether the number is armstrong number or not.
32- * @param num Number
33- * @return `true` if the number is armstrong.
34- * @return `false` if the number is not armstrong.
37+ * @brief Function to check whether the number is armstrong number or not.
38+ * @param number to be checked
39+ * @return `true` if the number is armstrong.
40+ * @return `false` if the number is not armstrong.
3541 */
3642bool is_armstrong (int number) {
37- // If the number is less than 0, then it is not a armstrong number.
43+ // If the number is less than 0, then it is not an armstrong number.
3844 if (number < 0 ) {
3945 return false ;
4046 }
47+
4148 int sum = 0 ;
4249 int temp = number;
4350 // Finding the total number of digits in the number
@@ -46,17 +53,17 @@ bool is_armstrong(int number) {
4653 int rem = temp % 10 ;
4754 // Finding each digit raised to the power total digit and add it to the
4855 // total sum
49- sum = sum + std::pow (rem, total_digits);
56+ sum += static_cast < int >( std::pow (rem, total_digits) );
5057 temp = temp / 10 ;
5158 }
5259 return number == sum;
5360}
5461
5562/* *
56- * Function for testing the is_armstrong() function
57- * with all the test cases.
63+ * @brief Self-test implementations
64+ * @returns void
5865 */
59- void test () {
66+ static void test () {
6067 // is_armstrong(370) returns true.
6168 assert (is_armstrong (370 ) == true );
6269 // is_armstrong(225) returns false.
@@ -72,9 +79,10 @@ void test() {
7279}
7380
7481/* *
75- * Main Function
82+ * @brief Main Function
83+ * @returns 0 on exit
7684 */
7785int main () {
78- test ();
86+ test (); // run self-test implementations
7987 return 0 ;
8088}
0 commit comments