3 - Armstrong number c sharp
Number is Armstrong in c#
public bool CheckArmstrongNumber(int num) {
int remainder, sum = 0;
for (int i = num; i > 0; i = i / 10)
{
remainder = i % 10;
sum = sum + remainder * remainder * remainder;
}
if (sum == num)
{
return true;
}
return false;
}
[TestMethod]
public void TestCheckArmstrongNumber()
{
BasicProgramming basicProgramming
= new BasicProgramming();
= new BasicProgramming();
bool output = basicProgramming
.CheckArmstrongNumber(153);
.CheckArmstrongNumber(153);
Assert.AreEqual(true, output);
}
How to check using dry run method if a number
is Armstrong or not.
is Armstrong or not.
Those numbers whose sum of the cube of its digits
is equal to that number.
is equal to that number.
For ex. 153 ^= cube
1^3 + 5^3 + 3^3 = 1 + 125 + 9 = 153
Others numbers : 370, 371, 407.
1 | public bool CheckArmstrongNumber(int num) { |
Comments
Post a Comment