-
Notifications
You must be signed in to change notification settings - Fork 353
/
Copy patharmstrong_number.cpp
42 lines (37 loc) · 942 Bytes
/
armstrong_number.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/*
In recreational number theory, a narcissistic number is also known
as a pluperfect digital invariant (PPDI), an Armstrong number or a
plus perfect number is a number that is the sum of its digits
each raised to the power of the number of digits.
*/
#include <bits/stdc++.h>
using namespace std;
// Function to check whether the Number is Armstrong Number or Not.
bool is_armstrong(int n) {
if (n < 0) {
return false;
}
int sum = 0;
int var = n;
int number_of_digits = floor(log10(n) + 1);
while (var > 0) {
int rem = var % 10;
sum = sum + pow(rem, number_of_digits);
var = var / 10;
}
return n == sum;
}
int main() {
cout << "Enter the Number to check whether it is Armstrong Number or Not:" << endl;
int n;
cin >> n;
if (is_armstrong(n))
cout << n << " is Armstrong Number." << endl;
else
cout << n << " is Not Armstrong Number." << endl;
return 0;
}
/*
Time Complexity: O(log(n))
Space Complexity: O(1)
*/