Reputation: 343
I am trying to generate armstrong number to nth number.So,I have written a code for that,but it is not working.Everytime I input a number like 999 or 10000,it return only 0....can anyone please help me to find out what's wrong with this code:
#include <stdio.h>
#include <math.h>
int main()
{
double remainder,n=0;
int number,sum = 0,q,x,count;
printf("Enter an integer upto which you want to find armstrong numbers:");
scanf("%d",&number);
printf("\nFollowing armstrong numbers are found from 1 to %d\n",number);
for( count = 1 ; count <= number ; count++ )
{
q = count;
x = count;
for(;x != 0;)
{
x =x / 10;
n = n + 1;
}
while(q != 0)
{
remainder = q % 10;
sum = sum +(pow(remainder,n));
q = q / 10;
}
if ( count == sum ){
printf("%d\n", count);
}
sum = 0;
}
return 0;
}
Upvotes: 0
Views: 165
Reputation: 5307
You have to initialize n
to zero inside the loop for count
. And take care of proper rounding of the result of pow
by for example adding 0.5
to it.
Upvotes: 1