An Armstrong number (also known as a narcissistic number) is a number that is equal to the sum of its own digits each raised to the power of the number of digits. For example, 153 is an Armstrong number because \(1^3 + 5^3 + 3^3 = 153\). Understanding Armstrong numbers is important in programming as they provide insights into properties of numbers and can be applied in various algorithmic challenges. This concept is often used in introductory programming exercises to help learners understand loops, conditionals, and number manipulation.
What is an Armstrong Number?
An Armstrong number is a special type of number that holds a unique property: the sum of its digits raised to the power of the number of digits equals the number itself. For instance, for the number 371, it has three digits, and \(3^3 + 7^3 + 1^3 = 371\). This concept is useful in various mathematical and computational contexts, especially in developing algorithms that filter or categorize numbers based on specific properties.
Syntax
// Function to check if a number is an Armstrong number
bool isArmstrong(int number) {
// Convert the number to string to get its digits
String numStr = number.toString();
// Get the number of digits
int length = numStr.length;
int sum = 0;
// Loop through each digit
for (var digit in numStr.split('')) {
// Convert digit back to integer and raise to the power of 'length'
sum += int.parse(digit).pow(length);
}
// Check if the sum equals the original number
return sum == number;
}
How It Works
- Input: We start with an integer number.
- Convert to String: The number is converted to a string to access individual digits.
- Count Digits: We calculate the total number of digits in the number.
- Calculate Sum: For each digit, we convert it back to an integer, raise it to the power of the number of digits, and add this to a cumulative sum.
- Comparison: Finally, we check if the calculated sum equals the original number to determine if it is an Armstrong number.
Example 1: Basic Usage
void main() {
int number = 153; // The number to check
// Calling the function to check if the number is an Armstrong number
bool result = isArmstrong(number);
// Printing the result
print('$number is an Armstrong number: $result');
}
Output:
153 is an Armstrong number: true
Explanation: In this example, we check if 153 is an Armstrong number and print the result, which is true.
Example 2: Intermediate Usage with Variations
void main() {
List<int> numbers = [0, 1, 2, 3, 10, 153, 370, 371, 9474]; // List of numbers to check
// Loop through each number in the list
for (var number in numbers) {
// Check if the current number is an Armstrong number
bool result = isArmstrong(number);
// Print the result for each number
print('$number is an Armstrong number: $result');
}
}
Output:
0 is an Armstrong number: true
1 is an Armstrong number: true
2 is an Armstrong number: true
3 is an Armstrong number: true
10 is an Armstrong number: false
153 is an Armstrong number: true
370 is an Armstrong number: true
371 is an Armstrong number: true
9474 is an Armstrong number: true
Explanation: Here, we check multiple numbers in a list to see which ones are Armstrong numbers and print the results.
Example 3: Real-World Application
void main() {
// Function to return a list of Armstrong numbers within a specified range
List<int> findArmstrongInRange(int start, int end) {
List<int> armstrongNumbers = []; // List to store Armstrong numbers
// Loop through the specified range
for (int i = start; i <= end; i++) {
if (isArmstrong(i)) {
armstrongNumbers.add(i); // Add to list if it's an Armstrong number
}
}
return armstrongNumbers; // Return the list of Armstrong numbers
}
// Finding Armstrong numbers between 1 and 1000
List<int> armstrongs = findArmstrongInRange(1, 1000);
// Print the found Armstrong numbers
print('Armstrong numbers between 1 and 1000: $armstrongs');
}
Output:
Armstrong numbers between 1 and 1000: [1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407]
Explanation: This code defines a function to find all Armstrong numbers within a specified range, which can be useful for mathematical applications or data validation.
When to Use Armstrong Number in Dart
| Topic | Description |
|---|---|
| Scenario 1 | In educational settings, to teach students about loops, conditionals, and number manipulation. |
| Scenario 2 | In coding interviews, where you may be asked to identify special properties of numbers. |
| Scenario 3 | In mathematical applications or games that involve number properties and classifications. |
Key Points
| Topic | Description |
|---|---|
| Definition | An Armstrong number equals the sum of its digits raised to their count. |
| Conversion | String conversion is essential for digit extraction. |
| Looping | Iterating through digits allows for calculations of powers. |
| Practical Use | Can be applied in algorithms, games, and educational contexts. |
| Multiple Checks | You can check multiple numbers using a loop. |
| Range Finding | Functions can be created to find Armstrong numbers within a specific range. |
This thorough exploration of Armstrong numbers in Dart not only lays the foundation for understanding this concept but also provides practical applications and code examples to solidify learning for beginners and intermediate programmers.