Search code examples
cfor-loopdigitspowkernighan-and-ritchie

Armstrong number program in C returns wrong value


I am writing a program to see if a user entered number is Armstrong or not, here is my code:

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

int main(){

int x = 0;

printf("Enter a natural number: ");
scanf("%d", &x);
int ans = x;

// Digit Counter
int counter = 0;          //Variable for number of digits in the user entered number
int b = x;                //For each time number can be divided by 10 and isnt 0
for (int i = 1; i <= x; i++){ // Then counter variable is incremented by 1
   b /= 10;
   if (b != 0){
     counter += 1;
   }            
}
++counter;

//Digit Counter
int sum = 0;
// Digit Finder
int D;
for (int j = 1; j <= x; j++){
   D = x % 10;               //Shows remainder of number (last digit) when divided by 10
   sum += pow(D, counter);   //Raises Digit found by counter and adds to sum
   printf("%d\n", sum);
   x /= 10;                  // Divides user entered number by 10 to get   rid of digit found
}



if (sum == ans){
   printf("%d is a Armstrong number! :)", ans);
}else 
   printf("%d is not an Armstrong number :(", ans);
   //Digit Finder

   return 0;
}

My problem is that the program works fine apart from one point, when the program is given a Armstrong number which does not start with 1 then it behaves normally and indicates if it is an Armstrong number or not, but when i input a Armstrong number which start with 1 then it will print out the Armstrong number but -1.

For example: If i input something such as 371 which is an Armstrong number it will show that it is an Armstrong number. However if i input 1634 it will output 1633 which is 1 less than 1634.

How can i fix this problem?, also by the way could someone comment on my code and tell me if it seems good and professional/efficient because i am a beginner in C and would like someone else's opinion on my code.


Solution

  • How can I fix this problem.

    You know the number of iterations you want to make once you have calculated the digit count. So instead of looping till you reach the value of x:

    for (int j = 1; j <= x; j++){
    

    use the digit counter instead:

    for (int j = 1; j <= counter; j++) {
    

    also by the way could someone comment on my code and tell me if it seems good and professional/efficient because i am a beginner in C and would like someone else's opinion on my code.

    There's a number of things you can do to improve your code.

    1. First and foremost, any piece of code should be properly indented and formatted. Right now your code has no indenting, which makes it more difficult to read and it just looks ugly in general. So, always indent your code properly. Use an IDE or a good text editor, it will help you.

    2. Be consistent in your code style. If you are writing

    if (some_cond) {
     ...
    }
    else
       //do this
    

    It is not consistent. Wrap the else in braces as well.

    1. Always check the return value of a function you use, especially for scanf. It will save you from many bugs in the future.
    
    if (scanf("%d", &x) == 1)
        //...all OK...
    else
       // ...EOF or conversion failure...
       exit(EXIT_FAILURE);
    
    1. Your first for loop will iterate x times uselessly. You can stop when you know that you have hit 0:
    for (int i = 1; i <= x; i++){ // Then counter variable is incremented by 1
       b /= 10;
       if (b == 0){
          break;
       }
       counter += 1;    
    }
    
    1. C has ++ operator. Use that instead of doing counter += 1

    2. int D; you create this, but don't initialize it. Always initialize your variables as soon as possible

    3. C has const qualifier keyword, which makes a value immutable. This makes your code more readable, as the reader can immediately tell that this value will not change. In your code, you can change ans variable and make it a const int because it never changes:

    const int ans = x;
    
    1. Use more descriptive names for your variables. ans, D don't tell me anything. Use proper names, so that the reader of your code can easily understand your code.

    These are some of the things that in my opinion you should do and keep doing to improve your code and coding skills. I am sure there can be more things though. Keep your code readable and as simple as possible.