- Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathKrishnamurthyNumberChecker.cs
37 lines (32 loc) · 950 Bytes
/
KrishnamurthyNumberChecker.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
usingSystem;
namespaceAlgorithms.Numeric;
/// <summary>
/// A Krishnamurthy number is a number whose sum of the factorial of digits
/// is equal to the number itself.
///
/// For example, 145 is a Krishnamurthy number since: 1! + 4! + 5! = 1 + 24 + 120 = 145.
/// </summary>
publicstaticclassKrishnamurthyNumberChecker
{
/// <summary>
/// Check if a number is Krishnamurthy number or not.
/// </summary>
/// <param name="n">The number to check.</param>
/// <returns>True if the number is Krishnamurthy, false otherwise.</returns>
publicstaticboolIsKMurthyNumber(intn)
{
intsumOfFactorials=0;
inttmp=n;
if(n<=0)
{
returnfalse;
}
while(n!=0)
{
intfactorial=(int)Factorial.Calculate(n%10);
sumOfFactorials+=factorial;
n=n/10;
}
returntmp==sumOfFactorials;
}
}