- Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathNarcissisticNumberChecker.cs
39 lines (34 loc) · 1 KB
/
NarcissisticNumberChecker.cs
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
usingSystem;
namespaceAlgorithms.Numeric;
/// <summary>
/// A Narcissistic number is equal to the sum of the cubes of its digits. For example, 370 is a
/// Narcissistic number because 3*3*3 + 7*7*7 + 0*0*0 = 370.
/// </summary>
publicstaticclassNarcissisticNumberChecker
{
/// <summary>
/// Checks if a number is a Narcissistic number or not.
/// </summary>
/// <param name="number">Number to check.</param>
/// <returns>True if is a Narcissistic number; False otherwise.</returns>
publicstaticboolIsNarcissistic(intnumber)
{
varsum=0;
vartemp=number;
varnumberOfDigits=0;
while(temp!=0)
{
numberOfDigits++;
temp/=10;
}
temp=number;
while(number>0)
{
varremainder=number%10;
varpower=(int)Math.Pow(remainder,numberOfDigits);
sum+=power;
number/=10;
}
returnsum==temp;
}
}