-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrongNumber.cs
More file actions
66 lines (58 loc) · 1.67 KB
/
Copy pathstrongNumber.cs
File metadata and controls
66 lines (58 loc) · 1.67 KB
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
58
59
60
61
62
63
64
65
66
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace strongNumber
{
class Program
{
static void Main(string[] args)
{
//strong number is the number that the sum of the factorial of its digits is equal to number itself
//example: 1! + 4! + 5! = 1 + 24 + 120 = 145
string result = Kata.StrongNumber(0);
Console.WriteLine(result);
}
}
class Kata
{
public static string StrongNumber(int number)
{
List<char> charList = new List<char>();
charList.AddRange(number.ToString());
List<int> toCalculateList = new List<int>();
foreach (var item in charList)
{
toCalculateList.Add(Convert.ToInt32(item.ToString()));
}
int sumFac = 0;
int saveNumber = number;
foreach (var item in toCalculateList)
{
int facNumber = Factorial(item);
Console.WriteLine(facNumber);
sumFac += facNumber;
}
if (sumFac == saveNumber)
return "STRONG!!!!";
else
return "Not Strong !!";
}
public static int Factorial(int number)
{
if (number == 0)
return 1;
else
{
int facNumber = 1;
while (number != 1)
{
facNumber = facNumber * number;
number = number - 1;
}
return facNumber;
}
}
}
}