
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
C++ Program to Display Armstrong Number Between Two Intervals
An armstrong number is a positive integer that is equal to the sum of its digits, each raised to the power of the total number of digits in the number. Our goal here is to display the armstrong numbers between two intervals in C++. In simple terms, for a number with n digits:
abcd... = a^n + b^n + c^n + d^n + ...
If this condition is satisfied, we can say the number is an armstrong number.
Let's understand with examples:
153 is an Armstrong number because: 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153 1634 is also an Armstrong number because: 1^4 + 6^4 + 3^4 + 4^4 = 1 + 1296 + 81 + 256 = 1634 But 123 is not an Armstrong number because: 1^3 + 2^3 + 3^3 = 1 + 8 + 27 = 36 ? 123
Display Armstrong Numbers Between Intervals
To display armstrong numbers between two intervals in C++, we need to check each number in the given range to see if it meets the armstrong condition.
Here are the steps we follow:
- We define a function that checks whether a number is an Armstrong number.
- In the main function, we loop through the numbers from the start to the end of the interval, calling this function for each number.
- Then, for each number, we count its digits and calculate the sum of each digit raised to the power of the total number of digits.
- If this sum equals the original number, it is an armstrong number.
- Finally, we display all the armstrong numbers found within the given interval.
C++ Program to Display Armstrong Numbers Between Intervals
Here's a complete C++ program where we display Armstrong numbers between two intervals:
#include <iostream> #include <cmath> using namespace std; // Function to check if a number is Armstrong bool isArmstrong(int num) { int sum = 0, temp, remainder, n = 0; // Store the number in a temporary variable temp = num; // Find the number of digits while (temp != 0) { temp /= 10; ++n; } temp = num; // Calculate the sum of the powers of digits while (temp != 0) { remainder = temp % 10; sum += pow(remainder, n); temp /= 10; } // If sum equals the original number, it's an Armstrong number return (sum == num); } int main() { int start = 100, end = 999; // Loop through the range and check for Armstrong numbers cout << "Armstrong numbers between " << start << " and " << end << " are: " << endl; for (int i = start; i <= end; ++i) { if (isArmstrong(i)) { cout << i << " "; } } return 0; }
The output below displays the armstrong numbers between the two intervals:
Armstrong numbers between 100 and 999 are: 153 370 371 407
Time Complexity: O(n * d * log d) because we check each number with d digits in the range n.
Space Complexity: O(1) because we only use a constant amount of extra space.