3

I'm trying to find whether a number is an armstrong number or not, but first, I want to find the armstring value (each digit of the number must be powered to the number of digits in the number). For instance, if 9474 was the value, we should do 9^4(4 is the number of digits in the number) + (4^4) + (7^4) + (4^4) = 9474.

Some useful facts about my code:

  1. digitcount counts the digit to be squared
  2. b is intended to be the armstrong value

Here is my code so far:

#include <stdio.h>
void main(){
    int b,remainder, digitcount, i, j;
    digitcount = 0;
    printf("Enter a value for i: ");
    scanf("%d", &i);
    b = 0;
    remainder = 1;
    while (i != 0){
        i = i/10;
        ++digitcount;
    }
    printf("%d", digitcount);
    for (j = 1; j <= digitcount; j++){
        while (i != 0){
            while (digitcount != 0){
                remainder = (i % 10)*= digitcount;
                b = b+remainder;
                i = i/10;
                digitcount--;
            }
        }
    }
    printf("%d", b);
    
} 

When I put in 9474 for this, my answer yields an error "Invalid value on assignement".

I've been trying to debug this, but to no avail. what should I change in my code?

Droid
  • 520
  • 1
  • 7

1 Answers1

0

what should I change in my code?

You should break it into smaller, simpler modules. For your problem:

An Armstrong number is a number that is the sum of its own digits each raised to the power of the number of digits, in a given base b.

You have to:

  1. Calculate the number of digits of a given number.
  2. Calculate the power of a given number to a given exponent.
  3. Break a number into digits.
  4. Calculate a sum of powers.

The first one is fairly easy to do:

int count_digits(int n)
{
    int ndigits = 0;
    
    while (n != 0) {
        n /= 10;
        ndigits += 1;
    }
    
    return ndigits;
}

The second one is done using exponentiation by squaring (thanks @Dúthomhas):

int int_pow(int base, unsigned exp)
{
    int result = 1;
    for (;;) {
        if (exp & 1)
            result *= base;
        exp >>= 1;
        if (!exp)
            break;
        base *= base;
    }

    return result;
}

Now, the third and fourth ones become more straightforward:

bool is_armstrong(int n)
{
    int ndigits = count_digits(n);
    int digitsum = 0;
    
    for (int i = n; i != 0; i /= 10) {
        int digit = i % 10;
        digitsum += int_pow(digit, ndigits);
    }
    
    return n == digitsum;
}

If you want to test it:

int main(void)
{
    int n = 9474;
    printf("Armstrong(%d) = %s\n", n, is_armstrong(n) ? "true" : "false");
}

Prints:

Armstrong(9474) = true
Zakk
  • 1,935
  • 1
  • 6
  • 17