- 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
Write a C# program to check if the entered number is Armstrong number?
A number is an Armstrong number if the sum of the cube of each digit of the number is equal to the number itself.
Here, we will find out the remainder and will sum it to the cube of remainder.
rem = i % 10; sum = sum + rem*rem*rem;
Then if the above sum that comes out after loop iteration is equal to the sum, then it will be an Armstrong number.
if (sum == num) { Console.Write("Armstrong Number!"); }
The following is an example −
Example
int num, rem, sum = 0; // checking for armstrong number num = 153; for (int i = num; i > 0; i = i / 10) { rem = i % 10; sum = sum + rem*rem*rem; } if (sum == num) { Console.Write("Armstrong Number!"); } else Console.Write("Not an Armstrong Number!"); Console.ReadLine();
Advertisements