You use i
in the control expression of the outer for
loop but in the inner loop you are modifying i
with the i=i/10;
.
Even a single execution of that line will ensure that the value of i
is lesser than its initial value.
Then this modified i
is incremented with the i++
in the outer for
loop.
If a
is less than b
, i<b
will always be true, resulting in an infinite loop.
But since you have the number in the form of string in word
, you could use that.
for(j=0;j<temp;j++)
{
int c=word[j]-48;
The -48
is used to convert the encoded character value (eg: 2 in ASCII is 50) to the actual number. If the encoding you use is not ASCII, you might have to do something different.
You could also make a copy of i
before entering the inner loop and use that variable instead of i
in the inner loop like
int t=i, j;
for(sum=j=0;j<temp;j++)
{
int c=t%10;
sum+=pow(c,temp);
t=t/10;
}
You are not resetting the value of sum
to 0
before each iteration of the inner loop. You could do that in the first part of the loop (ie, before the first semi-colon) as in the above loop.
Also as pointed out by Bo, pow()
returns a float
and due to reasons expounded here, inaccuracies might creep in.
So make the type of sum
to be float
instead of int
.
Use of void
as the return type of main()
is not considered good practice. Use int main()
instead. See here.
The part where you take the digits using %
operator is also a mistake.
int c=i%10;
On all iterations of the inner loop, value of c
would be the last digit of i
and you won't get any other digit in c
.