- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

An Armstrong Number is a number where the sum of the digits raised to the power of total number of digits is equal to the number.

Some examples of Armstrong numbers are as follows −

3 = 3^1 153 = 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153 407 = 4^3 + 0^3 + 7^3 = 64 +0 + 343 = 407 1634 = 1^4 + 6^4 + 3^4 + 4^4 = 1 + 1296 + 81 + 256 = 1634

A program that displays the Armstrong numbers between two intervals is as follows.

#include <iostream> #include <cmath> using namespace std; int main() { int lowerbound, upperbound, digitSum, temp, remainderNum, digitNum ; lowerbound = 100; upperbound = 500; cout<<"Armstrong Numbers between "<<lowerbound<<" and "<<upperbound<<" are: "; for(int num = lowerbound; num <= upperbound; num++) { temp = num; digitNum = 0; while (temp != 0) { digitNum++; temp = temp/10; } temp = num; digitSum = 0; while (temp != 0) { remainderNum = temp%10; digitSum = digitSum + pow(remainderNum, digitNum); temp = temp/10; } if (num == digitSum) cout<<num<<" "; } return 0; }

Armstrong Numbers between 100 and 500 are: 153 370 371 407

In the above program, Armstrong numbers between the given intervals are found. This is done using multiple steps. The lowerbound and upperbound of the interval are given. Using these, a for loop is started from lowerbound to upperbound and each number is evaluated to see if it is an Armstrong number or not.

This can be seen in the following code snippet.

lowerbound = 100; upperbound = 500; cout<<"Armstrong Numbers between "<<lowerbound<<" and "<<upperbound<<" are: "; for(int num = lowerbound; num <= upperbound; num++)

In the for loop, first the number of digits in the number i.e in num are found. This is done by adding one to digitNum for each digit.

This is demonstrated by the following code snippet.

temp = num; digitNum = 0; while (temp != 0) { digitNum++; temp = temp/10; }

After the number of digits are known, digitSum is calculated by adding each digit raised to the power of digitNum i.e. number of digits.

This can be seen in the following code snippet.

temp = num; digitSum = 0; while (temp != 0) { remainderNum = temp%10; digitSum = digitSum + pow(remainderNum, digitNum); temp = temp/10; }

If the number is equal to the digitSum, then that number is an Armstrong number and it is printed. If not, then it is not an Armstrong number. This is seen in the below code snippet.

if (num == digitSum) cout<<num<<" ";

- Related Questions & Answers
- C++ Program to Display Prime Numbers Between Two Intervals
- C++ Program to Display Prime Numbers Between Two Intervals Using Functions
- C program to display the prime numbers in between two intervals
- C++ Program to Check Armstrong Number
- C Program to Check Armstrong Number?
- Armstrong Numbers between two integers?
- Java program to print the Armstrong numbers between two numbers
- Python Program to Check Armstrong Number
- Write a C# program to check if the entered number is Armstrong number?
- C Program for Armstrong Numbers
- C# Program to Check Whether the Entered Number is an Armstrong Number or Not
- Armstrong number in Java.
- Armstrong Number in Python
- Java program to check whether the given number is an Armstrong number
- 8085 program to find smallest number between two numbers

Advertisements