Advertisement

Latest Post

Tuesday, 3 January 2017

How to print Armstrong numbers from 1 to 100 [C#]

Write a program in C# to print Armstrong numbers from 1 to 100.

Following is the simple C# code :
class Lab
    {
        static void Main(string[] args)
        {
            int i, num, Number, lastDigit, sum = 0;
            Console.WriteLine("Enter any number to find Armstrong number upto: ");
            Number = int.Parse(Console.ReadLine());
            Console.WriteLine("All Armstrong numbers between 1 to {0}:\n", Number);
            for (i = 1; i <= Number; i++)
            {
                num = i;
                sum = 0;
                while (num != 0)
                {
                    lastDigit = num % 10;
                    sum += lastDigit * lastDigit * lastDigit;
                    num = num / 10;
                }
                if (i == sum)
                {

                    Console.WriteLine("{0} is ARMSTRONG NUMBER\n", i);
                }
            }
            Console.ReadLine();
        }
    }

Output:-
Enter any number to find Armstrong number upto:
1000
All Armstrong numbers between 1 to 1000:

1 is ARMSTRONG NUMBER

153 is ARMSTRONG NUMBER

370 is ARMSTRONG NUMBER

371 is ARMSTRONG NUMBER

407 is ARMSTRONG NUMBER


Government Jobs