If you have started programming in C#, you might get a assignment or a learning task to create armstrong number program in C#, so in this article, you will see what is armstong number and how to create C# console application program in it.

Before we proceed, let's understand what is Armstrong number.

Armstrong number is a number that is equal to the sum of cubes of its digits. For example 407 is an Armstrong numbers, how?

(4*4*4) + (0*0*0) + (7*7*7) = 407 which is the number itself.

so what can be the logic to create this type of program?

Here are the steps:

  1. Get the number from user
  2. Loop through each digit of number and multiply by iteself and then again multiple by itself
  3. Sum each multiplication result
  4. If total Sum = number itself, then print number is armstrong else not.

Armstrong Number Program in C#

If you are using Visual Studio, then you can Open Visual Studio, click on "File"-> "New" -> "Project" -> Select "Windows Desktop" from left-pane and "Consol App" from right-pane -> Click "ok"

Then in Program.cs, use the below code

using System;

namespace ArmstrongNumber
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter a number:");
            int NumberToCheck= Convert.ToInt32( Console.ReadLine());

            Console.WriteLine(isArmstrong(NumberToCheck));
        }

        //main method to check if number is armstrong
        static string isArmstrong(int x)
        {
            int sum = 0;

            //loop through each number by dividing it by 10
            // by dividing it by 10, we get only number remainder (last remaining digit)
            for (int i = x; i > 0; i = i / 10)
            {
                sum = sum + (int)Math.Pow(i % 10, 3.0);
            }

            if (x == sum)
            {
                return "Number is armstrong number";
            }    
            else
            {
                return "Number is not armstrong number";
            }
              
        }
    }
}

Output:

Enter a number:
44
Number is not armstrong number

Another output:

Enter a number:
407
Number is armstrong number

That's it, if you have any questions, feel free to use our comments section.