This post is about writing a C# program to find Armstrong Number. This program may help you to clear your Interview if you are a job seek or if you are a teacher you may can use this code for teaching your students.
I have written the same program in PHP, Java, Python in the previous lessons, if you wish to see those just search “Armstrong” in the search box.
Here is the entire code for Armstrong number (Visual Studio C# Console program):
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 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Armstrong { class Program { static void Main(string[] args) { int num = 0, sum = 0, rem = 0, temp; Console.WriteLine("Enter Number"); //get the number and store it in num num = Convert.ToInt32(Console.ReadLine()); //assign it to temp variable temp = num; //loop till temp becomes 0 while (temp != 0) { rem = temp % 10; //find the reminder sum = sum + (rem * rem * rem); //cube reminder and add it to sum temp = temp / 10; //find quotient, if not 0 then loop again } if (num == sum) //if original value of num and found value of sum is equal then it is Armstrong { Console.WriteLine("Armstrong Number"); } else { Console.WriteLine("Not an Armstrong Number"); } Console.ReadKey(); } } } |
Create a new Console application in Visual studio and copy paste this code and press F5 to run this program.
These are the few Armstrong number which helps you to test this code: 153, 371, 407