Skip to content

Commit f2eb5f7

Browse files
authored
Merge pull request #362 from suprithars111/armstrong_number
feat: Add Armstrong Number
2 parents 8af0886 + 8714b46 commit f2eb5f7

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

Numbers/armstrong_number.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
*/

0 commit comments

Comments
 (0)