Armstrong number

An Armstrong number (also called a plus perfect number or narcissistic number) is a number equal to the sum of the nth power of the digits, where n is the quantity of digits in the number.

Check Armstrong Number

This is a C++ program that checks if a given number is an Armstrong number.

An Armstrong number is a number that is equal to the sum of its digits raised to the power of the number of digits.

The code has two functions:

  1. bool checkArmstrong(int number): This function takes an integer as input and returns a boolean value indicating whether the given number is an Armstrong number or not.

It starts by initializing three variables, temp, remainder, and result, to the input number. temp is used as a temporary variable to store the input number. counter is used to keep track of the number of digits in the input number. result is used to store the sum of the digits raised to the power of the number of digits.

In the first while loop, the number of digits in the input number is calculated by repeatedly dividing the temp by 10 until it becomes zero.

In the second while loop, the digits of the input number are extracted one by one, and their values raised to the power of the number of digits are added to result. This is done by using the modulo operator to extract the last digit, computing the power of that digit, and then updating temp to remove the last digit.

Finally, if the sum of the digits raised to the power of the number of digits is equal to the input number, the function returns true, indicating that the number is an Armstrong number. Otherwise, it returns false.

  1. int main(): This is the main function of the program, where the input is taken and the result of the checkArmstrong function is displayed.

The user is prompted to enter a number to check for being an Armstrong number. The input number is passed to the checkArmstrong function, which returns a boolean value indicating whether the number is an Armstrong number or not. The result is then displayed on the console.

#include <iostream>
using namespace std;

bool checkArmstrong(int number) {
    int temp, remainder, i, counter = 0, result = 0, power;
    temp = number;

    while (temp != 0) {
        temp /= 10;
        counter++;
    }
    temp = number;

    while (temp != 0) {
        remainder = temp % 10;

        power = 1;
        i = counter;
        while (i != 0) {
            power *= remainder;
            i--;
        }

        result += power;
        temp /= 10;
    }
    
    if (number == result) {
        return true;
    } else {
        return false;
    }
}

int main() {
    int number;

    cout << "Enter a number to check for armstrong:  ";
    cin >> number;

    if (checkArmstrong(number)) {
        cout << number << " is an armstrong number";
    } else {
        cout << number << " is not an armstrong number";
    }

    return 0;
}
Output
Enter a number to check for armstrong:  371
371 is an armstrong number

Armstrong Number Series

This is a C++ program to find all Armstrong numbers within a specified upper bound. An Armstrong number, also known as a narcissistic number, is a number that is equal to the sum of its own digits raised to the power of the number of digits.

The program defines two functions: checkArmstrong and main. The checkArmstrong function takes an integer number as input and returns a boolean value indicating whether or not the number is an Armstrong number. The main function is the entry point of the program and contains the main logic.

The checkArmstrong function first determines the number of digits in number using a while loop. Then it uses another while loop to calculate the sum of the cubes of the digits. If the sum of the cubes of the digits is equal to the original number, the function returns true. Otherwise, it returns false.

The main function first prompts the user to enter an upper bound. It then uses a for loop to iterate through all the numbers from 1 to the upper bound and check if each number is an Armstrong number. If a number is an Armstrong number, it is printed to the console.

Finally, the program returns 0, indicating that it has completed execution successfully.

#include &lt;iostream&gt;
using namespace std;

bool checkArmstrong(int number) {
    int temp, remainder, i, counter = 0, result = 0, power;
    temp = number;

    while (temp != 0) {
        temp /= 10;
        counter++;
    }
    temp = number;

    while (temp != 0) {
        remainder = temp % 10;

        power = 1;
        i = counter;
        while (i != 0) {
            power *= remainder;
            i--;
        }

        result += power;
        temp /= 10;
    }

    if (number == result) {
        return true;
    } else {
        return false;
    }
}

int main() {
    int max;

    cout << "Enter the upper bound to find armstrong numbers: ";
    cin >> max;

    cout << "Armstrong Number Series:";
    for (int i = 1; i <= max; i++) {
        if (checkArmstrong(i)) {
            cout << i << " ";
        }
    }

    return 0;
}
Output
Enter the upper bound to find armstrong numbers: 1000
Armstrong Number Series:1 2 3 4 5 6 7 8 9 153 370 371 407