ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

Published at Apr 17 2019
·
0 comments

Instructions

Test suite

Solution

An Armstrong number is a number that is the sum of its own digits each raised to the power of the number of digits.

For example:

- 9 is an Armstrong number, because
`9 = 9^1 = 9`

- 10 is
*not*an Armstrong number, because`10 != 1^2 + 0^2 = 1`

- 153 is an Armstrong number, because:
`153 = 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153`

- 154 is
*not*an Armstrong number, because:`154 != 1^3 + 5^3 + 4^3 = 1 + 125 + 64 = 190`

Write some code to determine whether a number is an Armstrong number.

To run the tests, run the command `dotnet test`

from within the exercise directory.

Initially, only the first test will be enabled. This is to encourage you to solve the exercise one step at a time.
Once you get the first test passing, remove the `Skip`

property from the next test and work on getting that test passing.
Once none of the tests are skipped and they are all passing, you can submit your solution
using `exercism submit ArmstrongNumbers.cs`

For more detailed information about the C# track, including how to get help if you're having trouble, please visit the exercism.io C# language page.

```
// This file was auto-generated based on version 1.0.0 of the canonical data.
using Xunit;
public class ArmstrongNumbersTest
{
[Fact]
public void Single_digit_numbers_are_armstrong_numbers()
{
Assert.True(ArmstrongNumbers.IsArmstrongNumber(5));
}
[Fact(Skip = "Remove to run test")]
public void There_are_no_2_digit_armstrong_numbers()
{
Assert.False(ArmstrongNumbers.IsArmstrongNumber(10));
}
[Fact(Skip = "Remove to run test")]
public void Three_digit_number_that_is_an_armstrong_number()
{
Assert.True(ArmstrongNumbers.IsArmstrongNumber(153));
}
[Fact(Skip = "Remove to run test")]
public void Three_digit_number_that_is_not_an_armstrong_number()
{
Assert.False(ArmstrongNumbers.IsArmstrongNumber(100));
}
[Fact(Skip = "Remove to run test")]
public void Four_digit_number_that_is_an_armstrong_number()
{
Assert.True(ArmstrongNumbers.IsArmstrongNumber(9474));
}
[Fact(Skip = "Remove to run test")]
public void Four_digit_number_that_is_not_an_armstrong_number()
{
Assert.False(ArmstrongNumbers.IsArmstrongNumber(9475));
}
[Fact(Skip = "Remove to run test")]
public void Seven_digit_number_that_is_an_armstrong_number()
{
Assert.True(ArmstrongNumbers.IsArmstrongNumber(9926315));
}
[Fact(Skip = "Remove to run test")]
public void Seven_digit_number_that_is_not_an_armstrong_number()
{
Assert.False(ArmstrongNumbers.IsArmstrongNumber(9926314));
}
}
```

```
ï»¿using System;
public static class ArmstrongNumbers
{
public static bool IsArmstrongNumber(int number)
{
int sum = 0;
Char[] numbers = number.ToString().ToCharArray();
foreach(char c in numbers)
{
Char.GetNumericValue(c);
sum += (int)(Math.Pow(Char.GetNumericValue(c), numbers.Length));
}
return sum == number;
}
}
```

A huge amount can be learned from reading other peopleâ€™s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

- What compromises have been made?
- Are there new concepts here that you could read more about to improve your understanding?

Level up your programming skills with 3,450 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors.
Exercism is
**100% free forever**.

## Community comments