- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to print all the Armstrong Numbers from 1 to 1000 using C#?
To display Armstrong numbers from 1 to 100, firstly use a while loop.
Example
while (val <= 1000) { }
Now inside the while loop, set conditions for first, second and third digit.
Example
d1 = val - ((val / 10) * 10); d2 = (val / 10) - ((val / 100) * 10); d3 = (val / 100) - ((val / 1000) * 10);
Since, Armstrong number checks for the cube of all the digits.
Example
res = (d1 * d1 * d1) + (d2 * d2 * d2) + (d3 * d3 * d3); if (res == val) { Console.WriteLine(temp); }
If the cube of all the digits is equal to the number itself, then the number is an Armstrong number, for example, 153.
Advertisements