Skip to content
doubtly

2 answers

Best answer

Answer: An n-digit number is Armstrong if the sum of each digit raised to the power n equals the number (e.g. \( 153 = 1^3 + 5^3 + 3^3 \)).

Logic: first count the digits d, then extract each digit with % 10, raise it to the power d and add; compare the sum with the original number.

Program:

#include <stdio.h>

int main()
{
    int n, temp, digits = 0, d, i, power, sum = 0;

    printf("Enter a number: ");
    scanf("%d", &n);

    /* Count the digits */
    temp = n;
    do
    {
        digits++;
        temp = temp / 10;
    } while (temp != 0);

    /* Add each digit raised to the power 'digits' */
    temp = n;
    while (temp != 0)
    {
        d = temp % 10;
        power = 1;
        for (i = 1; i <= digits; i++)
            power = power * d;
        sum = sum + power;
        temp = temp / 10;
    }

    if (sum == n)
        printf("%d is an Armstrong number\n", n);
    else
        printf("%d is not an Armstrong number\n", n);

    return 0;
}

Sample output (actual run):

Enter a number: 153
153 is an Armstrong number
Enter a number: 123
123 is not an Armstrong number
Doubtly · 23 Apr 2022 Report
Was this answer helpful?

A number is an Armstrong number if the sum of its digits, each raised to the power of the number of digits, equals the number itself (e.g. \(153 = 1^3 + 5^3 + 3^3\)).

#include <stdio.h>
int main() {
    int num, temp, rem, digits = 0, sum = 0, p, i;
    printf("Enter a number: ");
    scanf("%d", &num);

    temp = num;                 /* count the digits */
    while (temp != 0) {
        digits++;
        temp = temp / 10;
    }

    temp = num;                 /* add digit^digits */
    while (temp != 0) {
        rem = temp % 10;
        p = 1;
        for (i = 1; i <= digits; i++)
            p = p * rem;
        sum = sum + p;
        temp = temp / 10;
    }

    if (sum == num)
        printf("%d is an Armstrong number\n", num);
    else
        printf("%d is not an Armstrong number\n", num);
    return 0;
}

How it works:

  • The first loop counts the digits (for 153, digits = 3).
  • The second loop takes the last digit with % 10, raises it to that power, adds it to sum, and removes the digit with / 10.
  • Finally sum is compared with the original number: \(1 + 125 + 27 = 153\), so 153 is an Armstrong number; for 123, \(1 + 8 + 27 = 36 \ne 123\).

Other Armstrong numbers: 370, 371, 407, 1634.

Doubtly AI · 24 Oct 2026 AI-verified · Oct 2026 Report
Was this answer helpful?

Your answer

Log in or create a free account to answer this question.