Armstrong Number in CSharp – Code Optimized

Armstrong Number in CSharp – Code Optimized

Howdy Reader,

Wondering why a post again on Armstrong number? Well this post is not to duplicate the content already written for finding armstrong number in C# rather to list down some of the critiques (A detailed analysis and assessment) suggested by one of the senior developer – Andy Dennison

Armstrong Number in C# with code optimization Click To Tweet
Armstrong Number in CSharp - Code Optimized

Armstrong Number In C#

Critiques – The Analysis

Here is the detailed analysis of the example used in the previous post – armstrong number in C# with example.

#1. Input Validation

While seeking input from end-user we should always validated the input. In the example on my previous post we should use int.TryParse() method for validating the user input.
Reason: Parse throws an exception if it cannot parse the value, whereas TryParse returns a bool indicating whether it succeeded.

#2. The Decimal Division

Of the 4 basic operations, division is the slowest, often by an order of magnitude. If possible, it should be avoided. Instead use the input string directly and parse the characters.

#3. Using Console.WriteLine instead of String.Format

String.Format is slower and subject to run-time errors. There is no reason to use it when a “+” does the same thing.

#4. Not to mix UI with Algorithm

Should not be mixing the user interface with the algorithm. This makes the algorithm resistant to unit/automated tests.

#5. No Unit Tests

Andy says – “In an interview situation I at least ask if the interviewer wants to me to write see the unit tests first. If they love TDD, then they will be pleased. In an interview when I was asked to write a small method in Visual Studio (not a white board) to sum all of the primes from 1 to 1000, I wrote a unit test for one to 10. Since I knew that answer: 1 + 2 +3 + 5 +7, I could see if my method was correct at least for the simple case. Then I re-ran it for the requested case“.

Modifying the example from previous post would go like this:

Another alternative is to have the algorithm accept integers. Thus the validation is pushed up to the caller:

Note: The use of LINQ is not a requirement, but it helps to think of a decimal number as a string of digits in this case. It is a good example for beginners to start with.

Special Thanks & Credits

I would really like to thank Andy Dennison from bottom of my heart. He took out his time for giving his valuable inputs.


I hope you guys understood the logic behind finding Armstrong number in C# which I had already explained in my previous post. The focus of this post was merely to improve and optimize the code for finding Armstrong number.

What do you think?

Dear Reader,
If you have any questions or suggestions please feel free to email us or put your thoughts as comments below. We would love to hear from you. If you found this post or article useful then please share along with your friends and help them to learn.

Happy Reading:)