In this example, i’ll show you how to find the armstrong numbers from 1 to n in C# Console Application.
Find armstrong numbers between 1 to 100.
Find armstrong numbers between 1 to 500
Find armstrong numbers between 1 to 1000
Find armstrong numbers between 1 to 10000
Find armstrong numbers between 1 to 100000
Find armstrong numbers between 1 to 1000000
Find armstrong numbers between 1 to 10000000
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
class Program { static void Main(string[] args) { int num, originalNum, remainder, n = 0; double result = 0; Console.Write("Enter an number : "); num = Convert.ToInt32(Console.ReadLine()); // store the number of digits of num in n for (int i = 1; i <= num; i++) { originalNum = i; n = 0; result = 0; for (originalNum = i; originalNum != 0; ++n) { originalNum /= 10; } for (originalNum = i; originalNum != 0; originalNum /= 10) { remainder = originalNum % 10; // store the sum of the power of individual digits in result result += Math.Pow(remainder, n); } // if num is equal to result, the number is an Armstrong number if ((int)result == i) Console.WriteLine("{0} is an Armstrong number.", i); } Console.ReadKey(); } |
Output: