Check If Entered Number Is Armstrong Or Not In C#

 

Check If Entered Number Is Armstrong Or Not In C#

What is an Armstrong Number?

 
An Armstrong number is an n-digit number that is equal to the sum of the nth powers of its digits. A number that is the sum of its own digits each raised to the power of the number of digits. An Armstrong number of three digits is an integer where the sum of the cubes of its digits is equal to the number itself.

Let's try to understand why 371 is an Armstrong number.

      371 = (3*3*3)+(7*7*7)+(1*1*1)     

      where:     

      (3*3*3)=27     

      (7*7*7)=343     

      (1*1*1)=1     

      So:     

      27+343+1=371    


C# code

        private static void NumberArmstrong()

        {

            int number, rem, temp, sum = 0;

            Console.Write("Enter Your Number To Check :: ");

            number = int.Parse(Console.ReadLine());

 

            temp = number;

            while (number > 0)

            {

                rem = number % 10;

                sum = sum + (rem * rem * rem);

                number = number / 10;

            }

 

            if (temp == sum)

                Console.WriteLine(temp + " Is A Armstrong Number");

            else

                Console.WriteLine(temp + " Is Not A Armstrong Number");

            Console.ReadKey();

        }


Output





Share this

Related Posts

Previous
Next Post »