5 - Strong number in c sharp

Strong number in c sharp c#



public bool CheckStrongNumber(int n)
       {
           int i, n1, s1 = 0, j;
           int fact;
           n1 = n;
           for (j = n; j > 0; j = j / 10)
           {
               fact = 1;
               for (i = 1; i <= j % 10; i++)
               {
                   fact = fact * i;
               }
               s1 = s1 + fact;
           }
           if (s1 == n1)
           {
               return true;
           }
           return false;
       }
[TestMethod]
       public void TestCheckStrongNumber()
       {
           BasicProgramming basicProgramming
= new BasicProgramming();
           bool output = basicProgramming
.CheckStrongNumber(2);
           Assert.AreEqual(true, output);
       }
How to check or dry run to check whether
number is strong or not



A number is called strong number if sum of the factorial of its digit
is equals to number itself. For example 145 since
1! + 4! +5! = 1+24+120 =145


 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public bool CheckStrongNumber(int n)
{
int i, n1, s1 = 0, j;
int fact;
n1 = n;
for (j = n; j > 0; j = j / 10)
{
fact = 1;
for (i = 1; i <= j % 10; i++)
{
fact = fact * i;
}
s1 = s1 + fact;

}
if (s1 == n1)
{
return true;
}
return false;
}

Comments

Popular posts from this blog

7 - Sum of prime numbers 1 to N in c sharp

4 - Perfect number in c sharp