I am finding Armstrong number but it's showing "not a Armstrong number" Everytime and I can't find the solution. as my observation I think there is some problem the sum section. the sum is not giving the correct value.
#include<stdio.h>
#include<math.h>
int main()
{
int num, original_num, sum=0, lastdigit, digit, count=1;
printf("Enter the number: ");
scanf("%d", &num);
original_num = num;
while (num >= 10)
{
count ;
num /= 10;
}
digit = count;
while (num > 0)
{
lastdigit = num % 10;
sum = pow(lastdigit, digit); /** here is the problem lying as my
observation, the sum is giving the
incorrect value.**/
num/=10;
}
if (original_num == sum)
{
printf("The number is an ARMSTRONG number");
}
else
{
printf("The number is not a ARMSTRONG number");
}
return 0;
}
CodePudding user response:
num
has already became single digit after first while loop(which counts the number of digits). In which case your second loop which takes every digit in the number and raise it to the n power is running only once.
You need to restore the number before second loop.
num = original_num;
while (num > 0)
{
lastdigit = num % 10;
sum = pow(lastdigit, digit);
num/=10;
}