#include <iostream>
#include <cmath>
using namespace std;
// Function to count number of digits
int countDigits(int num) {
int count = 0;
while (num != 0) {
num /= 10;
count++;
}
return count;
}
// Function to check if number is Armstrong
bool isArmstrong(int num) {
int original = num;
int sum = 0;
int digits = countDigits(num);
while (num > 0) {
int digit = num % 10;
sum += pow(digit, digits); // digit raised to power of total digits
num /= 10;
}
return (sum == original);
}
// Function to display detailed calculation
void displayArmstrongCheck(int num) {
int original = num;
int sum = 0;
int digits = countDigits(num);
cout << "\n***** CHECKING " << num << " *****\n";
cout << "Number of digits: " << digits << endl;
cout << "\nCalculation:\n";
int temp = num;
while (temp > 0) {
int digit = temp % 10;
int power = pow(digit, digits);
cout << digit << "^" << digits << " = " << power << endl;
sum += power;
temp /= 10;
}
cout << "\nSum = " << sum << endl;
cout << "Original number = " << original << endl;
if (sum == original) {
cout << "\n✓ " << original << " is an ARMSTRONG NUMBER!\n";
} else {
cout << "\n✗ " << original << " is NOT an Armstrong number.\n";
}
}
// Function to find all Armstrong numbers in a range
void findArmstrongInRange(int start, int end) {
cout << "\nArmstrong numbers between " << start << " and " << end << ":\n";
bool found = false;
for (int i = start; i <= end; i++) {
if (isArmstrong(i)) {
cout << i << " ";
found = true;
}
}
if (!found) {
cout << "None found";
}
cout << endl;
}
int main() {
int number, choice;
cout << "========== ARMSTRONG NUMBER CHECKER ==========\n";
cout << "\nWhat is an Armstrong Number?\n";
cout << "A number is Armstrong if the sum of its digits\n";
cout << "raised to the power of number of digits equals\n";
cout << "the number itself.\n";
cout << "\nExamples:\n";
cout << " 153 = 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153 ✓\n";
cout << " 9474 = 9^4 + 4^4 + 7^4 + 4^4 = 6561 + 256 + 2401 + 256 = 9474 ✓\n";
cout << "\n\n1. Check a single number\n";
cout << "2. Find Armstrong numbers in a range\n";
cout << "Enter your choice: ";
cin >> choice;
if (choice == 1) {
cout << "\nEnter a number: ";
cin >> number;
if (number < 0) {
cout << "\nArmstrong numbers are defined for positive integers only.\n";
return 1;
}
displayArmstrongCheck(number);
}
else if (choice == 2) {
int start, end;
cout << "\nEnter start of range: ";
cin >> start;
cout << "Enter end of range: ";
cin >> end;
findArmstrongInRange(start, end);
}
else {
cout << "\nInvalid choice!\n";
return 1;
}
// Additional examples
cout << "\n\n***** COMMON ARMSTRONG NUMBERS *****\n";
cout << "Single digit (1-9): All are Armstrong numbers\n";
cout << "Three digit: 153, 370, 371, 407\n";
cout << "Four digit: 1634, 8208, 9474\n";
cout << "Five digit: 54748, 92727, 93084\n";
// Quick test with known Armstrong numbers
cout << "\n\n***** QUICK VERIFICATION *****\n";
int testNumbers[] = {0, 1, 9, 153, 370, 371, 407, 1634, 123, 100};
for (int i = 0; i < 10; i++) {
int num = testNumbers[i];
if (isArmstrong(num)) {
cout << num << " is Armstrong ✓\n";
} else {
cout << num << " is NOT Armstrong ✗\n";
}
}
return 0;
}
Comments
Post a Comment