Armstrong Number1


Submit solution

Points: 10 (partial)
Time limit: 8.0s
Memory limit: 64M

Author:
Problem type
Allowed languages
C

Get a sequence of numbers from the user and check if those numbers are armstrong numbers. Armstrong number is a number that is equal to the sum of cubes of its digits.

Sample Input

3

122

370

371

Sample Output

No

Yes

Yes

Explanation

The first line in the Sample Input contains the number of inputs given, Your program should work for any number of inputs. The next lines contain the input values. The Sample Output has the value Yes if armstrong number, otherwise No.


Comments


  • 0
    CBENU4CSE22433  commented on June 27, 2023, 2:13 p.m.

    include <stdio.h>

    include <math.h>

    int is_armstrong_recursive(int number, int originalNumber, int digitCount) { if (number == 0) { return originalNumber == 0; }

    int digit = number % 10;
    int poweredDigit = pow(digit, digitCount);
    return poweredDigit + is_armstrong_recursive(number / 10, originalNumber - poweredDigit, digitCount);

    }

    int is_armstrong(int number) { int digitCount = 0; int temp = number;

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

    }

    int main() { int number; printf("Enter a number: "); scanf("%d", &number);

    if (is_armstrong(number)) {
        printf("%d is an Armstrong number.\n", number);
    } else {
        printf("%d is not an Armstrong number.\n", number);
    }
    
    return 0;

    }