Home > Net >  What's wrong with this program to check the whether or not a number is an Armstrong number?
What's wrong with this program to check the whether or not a number is an Armstrong number?

Time:07-19

I was making a program to check whether a given number is an Armstrong number or not, but it is not working correctly. I had used two print statements to check how much of my code is working but they are showing value of count and total as 0. I don't know what is going wrong here.

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

 int main ()
 {
  //Program to check if a given number is Armstrong or not.
  int num, count, remain, total = 0;
  int onum = num;
  printf ("Enter a number:\n");
  scanf ("%d", &num);
  while (onum != 0)
    {
      onum = onum / 10;
      count  ;
    }
  printf ("Value stored at count is %d\n", count);
  onum = num;
  for (int i = 0; i < count; i  )
    {
        remain = onum % 10;
        total  = pow (remain, count);
        onum = onum / 10;
    }
  printf ("Value stored at Total is %d\n", total);
  if (num == total)
    {
        printf ("The entered number is an Armstrong\n");
    }
  else
    {
        printf ("The entered number is not an Armstrong\n");
    }
  return 0;
}

CodePudding user response:

int onum = num;

is bad. Variables in C are not automatically updated after assignment. You should assign num to onum only after reading a value to num.

Also you have to initialize count before performing arithmetics with the value of the variable.

The part

  int onum = num;
  printf ("Enter a number:\n");
  scanf ("%d", &num);

should be:

  int onum;
  printf ("Enter a number:\n");
  scanf ("%d", &num);
  onum = num;
  count = 0;

Also it is better to check the return value of scanf() to see if it successfully read what should be read:

  int onum;
  printf ("Enter a number:\n");
  if (scanf ("%d", &num) != 1)
    {
      fputs("read error\n", stderr);
      return 1;
    }
  onum = num;
  count = 0;

CodePudding user response:

I figured out my mistake thank you all to help me. The thing I did wrong was not initializing my variable with zero and

int onum = num;

I did this after getting the value of num first. I already knew it but somehow still did a mistake. And, the last thing is rounding of the remainder before using it in the pow function.

round(re);

The correct code is given below.

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

    int
    main ()
    {
      //Program to check if a given number is Armstrong or not.
      int num = 0, count = 0, remain = 0, total = 0;
      printf ("Enter a number:\n");
      scanf ("%d", &num);
      int onum = num;
      while (onum != 0)
        {
          onum = onum / 10;
          count  ;
        }
      printf ("Value stored at count is %d\n", count);
      onum = num;
      for (int i = 0; i < count; i  )
        {
            remain = onum % 10;
             int re = round(remain);
            total  = pow (re, count);
            onum = onum / 10;
        }
      printf ("Value stored at Total is %d\n", total);
      if (num == total)
        {
            printf ("The entered number is an Armstrong\n");
        }
      else
        {
            printf ("The entered number is not an Armstrong\n");
        }
      return 0;
    }
  •  Tags:  
  • c
  • Related