forked from TheAlgorithms/C-Sharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NarcissisticNumberChecker.cs
40 lines (36 loc) · 1.13 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
40
using System;
namespace Algorithms.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>
public static class NarcissisticNumberChecker
{
/// <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>
public static bool IsNarcissistic(int number)
{
var sum = 0;
var temp = number;
var numberOfDigits = 0;
while (temp != 0)
{
numberOfDigits++;
temp /= 10;
}
temp = number;
while (number > 0)
{
var remainder = number % 10;
var power = (int)Math.Pow(remainder, numberOfDigits);
sum += power;
number /= 10;
}
return sum == temp;
}
}
}