File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ /*
2+ In recreational number theory, a narcissistic number is also known
3+ as a pluperfect digital invariant (PPDI), an Armstrong number or a
4+ plus perfect number is a number that is the sum of its digits
5+ each raised to the power of the number of digits.
6+ */
7+
8+ #include < bits/stdc++.h>
9+ using namespace std ;
10+
11+ // Function to check whether the Number is Armstrong Number or Not.
12+
13+ bool is_armstrong (int n) {
14+ if (n < 0 ) {
15+ return false ;
16+ }
17+ int sum = 0 ;
18+ int var = n;
19+ int number_of_digits = floor (log10 (n) + 1 );
20+ while (var > 0 ) {
21+ int rem = var % 10 ;
22+ sum = sum + pow (rem, number_of_digits);
23+ var = var / 10 ;
24+ }
25+ return n == sum;
26+ }
27+
28+ int main () {
29+ cout << " Enter the Number to check whether it is Armstrong Number or Not:" << endl;
30+ int n;
31+ cin >> n;
32+ if (is_armstrong (n))
33+ cout << n << " is Armstrong Number." << endl;
34+ else
35+ cout << n << " is Not Armstrong Number." << endl;
36+ return 0 ;
37+ }
38+
39+ /*
40+ Time Complexity: O(log(n))
41+ Space Complexity: O(1)
42+ */
You can’t perform that action at this time.
0 commit comments