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:
Here’s how to implement a C++ program to check if a given number is an Armstrong number.
1️⃣ Example: Check if a Number is an Armstrong Number
This example will take an integer as input, calculate the sum of its digits raised to the power of the number of digits, and check if it equals the original number.
#include <iostream>
#include <cmath>
using namespace std;
int main() {
int N, originalNumber, sum = 0, remainder, numDigits;
// Taking input from the user
cout << "Enter a positive integer: ";
cin >> N;
originalNumber = N;
numDigits = 0;
// Counting the number of digits
while (N != 0) {
N /= 10;
numDigits++;
}
N = originalNumber; // Restore the original number
// Calculating the sum of each digit raised to the power of numDigits
while (N != 0) {
remainder = N % 10;
sum += pow(remainder, numDigits); // Raise the digit to the power of numDigits
N /= 10;
}
// Checking if the sum equals the original number
if (sum == originalNumber) {
cout << originalNumber << " is an Armstrong number." << endl;
} else {
cout << originalNumber << " is not an Armstrong number." << endl;
}
return 0;
}
Explanation:
- Input: The program asks the user to input a positive integer
N
. - Digit Count: It first calculates the number of digits in
N
by repeatedly dividingN
by 10 until it becomes 0. - Armstrong Calculation: Then, it calculates the sum of each digit raised to the power of the total number of digits.
- Comparison: Finally, it checks if the sum equals the original number and prints the result.
Output (Example when N = 153
):
Enter a positive integer: 153
153 is an Armstrong number.
2️⃣ Example: Checking Armstrong Number for Multiple Inputs
You can modify the program to check for Armstrong numbers multiple times or for a range of numbers. Here’s an example to check for Armstrong numbers from 1
to 999
:
#include <iostream>
#include <cmath>
using namespace std;
bool isArmstrong(int num) {
int originalNumber = num, sum = 0, remainder, numDigits = 0;
// Counting the number of digits
while (num != 0) {
num /= 10;
numDigits++;
}
num = originalNumber; // Restore the original number
// Calculating the sum of each digit raised to the power of numDigits
while (num != 0) {
remainder = num % 10;
sum += pow(remainder, numDigits);
num /= 10;
}
return (sum == originalNumber); // Return true if it's an Armstrong number
}
int main() {
cout << "Armstrong numbers between 1 and 999 are: " << endl;
for (int i = 1; i <= 999; i++) {
if (isArmstrong(i)) {
cout << i << " ";
}
}
cout << endl;
return 0;
}
Explanation:
- The
isArmstrong()
function checks if a number is an Armstrong number. - The
main()
function prints all Armstrong numbers between 1 and 999 by callingisArmstrong()
for each number in the range.
Output:
Armstrong numbers between 1 and 999 are:
1 153 370 371 407
3️⃣ Key Points to Remember:
- An Armstrong number of
n
digits is a number that is equal to the sum of its own digits each raised to the power ofn
. - The program uses
pow()
from<cmath>
to raise each digit to the power of the number of digits. - The method works for numbers with any number of digits, not just 3-digit numbers.
Would you like to test this with more examples or explore related topics like Palindrome numbers or Perfect numbers?
0 Comments