On Aug 30, 11:06 am, Mohit Gupta <mohitgupta.n...@gmail.com> wrote:
> *1.*
> /* Print armstrong numbers from 1 to 500 */
> /*1st version of prgrm: I am using pow function*/
> #include<stdio.h>
> #include<conio.h>
> #include<math.h>
> int main()
> {
> int num=1,temp,sum,r;
> while(num<=500){
>   sum=0;
>   temp=num;
>   while(temp){
>     r=temp%10;
>     sum+=pow(r,3);
>     temp/=10;
>   }
>   if(sum==num)
>     printf("%d\n",num);
>   num++;}
>
> getch();
> return 0;
>
> }
>
> It prints :
> 1
> 370
> 371
> 407
>
> But it does not print 153 which is also armstrong number. WHY???
>
> BUT if I change:  pow(r,3) to r*r*r in code....then it prints:
> 1
> 153
> 370
> 371
> 407
>
> WHY 153 was not printed if i use pow() function???


try this http://pastebin.com/cqc80i5M see the pow returns a int value
of 124 thus the sums answer turns to be 151 ....For correcting this
bug just use "double sum;" instead of "int sum" .And also refer this
two site to understand why this happens
http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm
http://c-faq.com/~scs/cgi-bin/faqcat.cgi?sec=fp
. http://download.oracle.com/docs/cd/E19957-01/806-3568/ncg_goldberg.html

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.

Reply via email to