Objects in this mirror are closer to Microsoft Technologies. DNM objective is to help users on Microsoft technologies by providing Articles, snippets, Interview Questions.

02 June 2012

.NET program to check whether given number is Armstrong number or not


In this snippet we will write the program for Armstrong number.

What is Armstrong Number?
The sum of the cubes of individual digits of a number is equal to the same number.
Example1: Number 153 => 13 + 53 +33 = 1+125+27= 153
so 153 is Armstrong number. 

Example2: Number 123 => 13 + 23 +33 = 1+8+27= 36. 36 is not equal to 123. 
So 123 is not a Armstrong number.

Snippet:
    class ArmStrongNumber
    {
        public static void Main()
        {
            Console.WriteLine("********** ArmStrong Number Check Example ********");
            Console.WriteLine("Enter Number to Want to check");
            int numberToCheck = int.Parse(Console.ReadLine());
            int lenthOfNumber = numberToCheck.ToString().Length;
            double reminder = 0;
            double sum = 0;
            int tempNo = numberToCheck;
            while (tempNo > 0)
            {
                reminder = tempNo % 10;
                sum = sum + Math.Pow(reminder, lenthOfNumber);
                tempNo = tempNo / 10;
            }
            if (sum == numberToCheck)
            {
                Console.WriteLine("The given Number {0} is a Armstrong Number", numberToCheck);
            }
            else
            {
                Console.WriteLine("The given Number {0} is NOT a Armstrong Number", numberToCheck);
            }
            Console.ReadLine();
        }

    }

Output:
Example : Input =153






Example : Input=123

Some of Armstrong numbers are 153, 370, 371, 407, 1634, 8208, 9474, 54748, 92727, 93084, 548834, 1741725, 4210818, 9800817, 9926315, 24678050, 24678051, 88593477, 146511208, 472335975, 534494836, 912985153, 4679307774.

0 Comments:

Post a Comment