c-plus-plus
  1. c-plus-plus-armstrong-number

C++ Program for Armstrong Number

An Armstrong number is a number that is equal to the sum of its own digits raised to the power of the number of digits. For example, 370 is an Armstrong number because 370 = 3^3 + 7^3 + 0^3 = 27 + 343 + 0.

Syntax

int isArmstrong(int num);

Example

#include <iostream>
#include <cmath>
using namespace std;

int isArmstrong(int num) {
    int temp = num;
    int sum = 0;
    int digitCount = 0;

    // Count the number of digits in the number
    while (temp != 0) {
        digitCount++;
        temp /= 10;
    }

    // Calculate the sum of each digit raised to the power of digit count
    temp = num;
    while (temp != 0) {
        int digit = temp % 10;
        sum += pow(digit, digitCount);
        temp /= 10;
    }

    // If the sum is equal to the original number, it is an Armstrong number
    if (sum == num) {
        return 1;
    } else {
        return 0;
    }
}

int main() {
    int num;
    cout << "Enter a number: ";
    cin >> num;

    if (isArmstrong(num)) {
        cout << num << " is an Armstrong number." << endl;
    } else {
        cout << num << " is not an Armstrong number." << endl;
    }

    return 0;
}

Output

Enter a number: 153
153 is an Armstrong number.

Explanation

In the above example, we have defined a function called isArmstrong which takes a number as input and returns 1 if it is an Armstrong number or 0 if it is not.

The function first counts the number of digits in the input number using a while loop. It then calculates the sum of each digit raised to the power of the digit count using another while loop. If the sum is equal to the original number, the function returns 1, indicating that it is an Armstrong number. Otherwise, it returns 0.

In the main function, we take input from the user and call the isArmstrong function to check if the number is an Armstrong number or not.

Use

The Armstrong number program is a simple example of a program that uses loops and conditional statements to solve a problem. It is often used as a beginner-level exercise to teach these basic programming concepts.

Important Points

  • An Armstrong 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 uses loops and conditional statements to count the number of digits in the input number and calculate the sum of each digit raised to the power of the digit count.
  • The pow function from the cmath library is used to raise each digit to the power of the digit count.
  • The program returns 1 if the input number is an Armstrong number and 0 if it is not.

Summary

In summary, the Armstrong number program is a simple example of a C++ program that uses loops and conditional statements to solve a mathematical problem. The program counts the number of digits in the input number and calculates the sum of each digit raised to the power of the digit count. It then checks if the sum is equal to the original number to determine if it is an Armstrong number or not.

Published on: