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 | public bool CheckStrongNumber(int n) |
Comments
Post a Comment