1
1
/* *
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
15
22
16
23
/* *
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.
18
25
* @param num Number
19
26
* @return Total number of digits.
20
27
*/
@@ -28,16 +35,17 @@ int number_of_digits(int num) {
28
35
}
29
36
30
37
/* *
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
33
40
* @return `true` if the number is armstrong.
34
41
* @return `false` if the number is not armstrong.
35
42
*/
36
43
bool 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.
38
45
if (number < 0 ) {
39
46
return false ;
40
47
}
48
+
41
49
int sum = 0 ;
42
50
int temp = number;
43
51
// Finding the total number of digits in the number
@@ -46,17 +54,17 @@ bool is_armstrong(int number) {
46
54
int rem = temp % 10 ;
47
55
// Finding each digit raised to the power total digit and add it to the
48
56
// total sum
49
- sum = sum + std::pow (rem, total_digits);
57
+ sum += static_cast < int >( std::pow (rem, total_digits) );
50
58
temp = temp / 10 ;
51
59
}
52
60
return number == sum;
53
61
}
54
62
55
63
/* *
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 () {
60
68
// is_armstrong(370) returns true.
61
69
assert (is_armstrong (370 ) == true );
62
70
// is_armstrong(225) returns false.
@@ -69,12 +77,15 @@ void test() {
69
77
assert (is_armstrong (0 ) == true );
70
78
// is_armstrong(12) returns false.
71
79
assert (is_armstrong (12 ) == false );
80
+
81
+ std::cout << " All tests have successfully passed!\n " ;
72
82
}
73
83
74
84
/* *
75
- * Main Function
76
- */
85
+ * @brief Main Function
86
+ * @returns 0 on exit
87
+ */
77
88
int main () {
78
- test ();
89
+ test (); // run self-test implementations
79
90
return 0 ;
80
91
}
0 commit comments