forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
armstrong_number.cpp
57 lines (51 loc) · 1.14 KB
/
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*
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;
}
/*
Input:
Enter the Number to check whether it is Armstrong Number or Not:
153
Output:
153 is Armstrong Number.
Input:
Enter the Number to check whether it is Armstrong Number or Not:
12
Output:
12 is Not Armstrong Number.
Time Complexity: O(log(n))
Space Complexity: O(1)
*/