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 it has 3 digits, and if you take each digit raised to the power of 3 and sum them, you get 153 (1^3 + 5^3 + 3^3 = 153). This concept is frequently explored in computer science education to teach loops, conditionals, and the manipulation of numbers in various programming languages including C++.
In this article, you will learn how to craft a C++ program to identify if a number is an Armstrong number. Mastery of this task strengthens your grasp of fundamental C++ operations, conditional logic, and the usage of mathematical functions. Various examples will elucidate the step-by-step process to build an effective program.
Assume the number 153
which you need to verify.
Analyze the steps to perform the verification:
#include <iostream>
#include <cmath> // for pow function
bool isArmstrong(int num) {
int originalNum = num, sum = 0, digits = 0;
// Counting the number of digits
for (int n = num; n > 0; n /= 10) {
digits++;
}
// Calculating the sum of digits raised to the power of the count of digits
for (int n = num; n > 0; n /= 10) {
int digit = n % 10;
sum += pow(digit, digits);
}
// Returning true if the sum of digits^digits == original num
return sum == originalNum;
}
int main() {
int num = 153;
if (isArmstrong(num)) {
std::cout << num << " is an Armstrong number." << std::endl;
} else {
std::cout << num << " is not an Armstrong number." << std::endl;
}
}
This code snippet defines a function isArmstrong()
that implements the logic for checking an Armstrong number. Notice the loop for digit extraction and the power computation, which are pivotal in the calculation.
It's beneficial to test the program with multiple examples, including edge cases like single-digit numbers and large numbers.
Integrate additional examples directly into your main function or through unit testing.
int main() {
// Examples to test the isArmstrong function
int testNumbers[] = {1, 153, 370, 371, 9474, 9475};
for (int num : testNumbers) {
if (isArmstrong(num)) {
std::cout << num << " is an Armstrong number." << std::endl;
} else {
std::cout << num << " is not an Armstrong number." << std::endl;
}
}
}
This script tests numbers from the array testNumbers
to check which ones are Armstrong numbers. The output helps verify the accuracy of our function across a spectrum of numbers.
The task of determining whether a number is an Armstrong number is more than an exercise in using mathematical operations—it also empowers you with deeper insights into the control structures within C++. By following the steps outlined and experimenting with the provided code examples, you get a robust introduction to combining iteration, conditional evaluation, and mathematical functions in C++. As you develop more complex programs, the essential skills demonstrated here ensure your code remains efficient and your logical implementation sound.