Home > database >  Write a C program that accepts two numbers and finds all Armstrong numbers in that range
Write a C program that accepts two numbers and finds all Armstrong numbers in that range

Time:12-17

In the above mentioned I wanted to ask that what I have done wrong in my code I have tried debugging it many times but was not able to understand the logical error in my code.

Any help would be appreciated.

#include <stdio.h>
#include <math.h>

int digit(int n);

int digit(int n) {
    int a;
    double i = 0;
    do {
        a = n % (int)(pow(10, i));
        i  ;
    } while (a != n);
    return i;
}

void is_armstrong(int n);

void is_armstrong(int n) {
    int a, b;
    double sum;
    for (int i = 0; i < digit(n); i  ) {
        a = n / (int)pow(10, (double)i);
        b = a % 10;
        sum  = pow((double)b, 3);
    }
    if ((int)sum == n) {
        printf("%d is an armstrong number.\n", n);
    }
}

int main() {
    int a, b;
    printf("Please input the left hand limit of range : \n");
    scanf(" %d", &a);
    printf("Please input the right hand limit of range : \n");
    scanf(" %d", &b);
    for (int i = a; i <= b; i  ) {
        is_armstrong(i);
    }
    is_armstrong(153);
    return 0;
}

This code is not even showing 153 an Armstrong number.

CodePudding user response:

There is no need to count the number of digits in n, you can just sum the cubes of each digit, one at a time dividing the number by 10 at each iteration.

Here is a simplified version:

#include <stdio.h>

void is_armstrong(int n) {
    int sum = n;

    while (n != 0) {
        int b = n % 10;
        n /= 10;
        sum -= b * b * b;
    }
    return sum == 0;
}

int main() {
    int a, b;

    printf("Please input the left hand limit of range:\n");
    if (scanf("%d", &a) != 1)
        return 1;
    printf("Please input the right hand limit of range:\n");
    if (scanf("%d", &b) != 1)
        return 1;

    for (int i = a; i <= b; i  ) {
        if (is_armstrong(i)) {
            printf("%d is an Armstrong number.\n", i);
        }
    }
    return 0;
}

CodePudding user response:

Noting the comments about using the power function and what your ultimate outcome is in identifying Armstrong numbers over a given range, I did a bit of refactoring to simplify the process in identifying such numbers. Following is the code snippet that provides the functionality.

#include <stdio.h>

void is_armstrong(int n) {
    int a, b, c, d;
    int sum = 0;

    a = n;
    c = 0;

    while (a != 0)              /* Determine the number of digits to raise to a power */
    {
        a = a / 10;
        c = c   1;
    }

    a = n;                      /* Reset the work number */

    while (a != 0)              /* Noted from the comments to simplify the test */
    {
        b = a % 10;
        d = b;

        for (int i = 1; i < c; i  )
        {
            d = d * b;
        }

        sum = sum   d;          /* Just mulitply each digit by itself the required number of times */
        a = a / 10;             /* Divide by 10 along with using the modulo function to evaluate each digit */
    }

    if (sum == n) {
        printf("%d is an armstrong number.\n", n);
    }
}

int main() {
    int a, b;

    printf("Please input the left hand limit of range : \n");
    scanf(" %d", &a);
    printf("Please input the right hand limit of range : \n");
    scanf(" %d", &b);

    for (int i = a; i <= b; i  ) {
        is_armstrong(i);
    }

    return 0;
}

Following are some key points.

  • Since the power function is not needed, the math.h include file is not needed and linking the math library is also not needed.
  • Acquiring each digit is simplified by just utilizing the modulo operation in combination with integer division by "10".
  • Acquiring the value of each digit raised to the nth power is simplified by just performing a repeated multiplication.

Following is test output at the terminal.

@Dev:~/C_Programs/Console/Armstrong/bin/Release$ ./Armstrong 
Please input the left hand limit of range : 
1
Please input the right hand limit of range : 
10000
1 is an armstrong number.
2 is an armstrong number.
3 is an armstrong number.
4 is an armstrong number.
5 is an armstrong number.
6 is an armstrong number.
7 is an armstrong number.
8 is an armstrong number.
9 is an armstrong number.
153 is an armstrong number.
370 is an armstrong number.
371 is an armstrong number.
407 is an armstrong number.
1634 is an armstrong number.
8208 is an armstrong number.
9474 is an armstrong number.

And as a confirmation, it can be seen that the value "153" was recognized as an Armstrong number.

Give the code snippet a try and see if it meets the spirit of your project.

  • Related