first TEN armstrong numbers

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Jan 2008
Posts: 11
Reputation: Esmerelda is an unknown quantity at this point 
Solved Threads: 0
Esmerelda's Avatar
Esmerelda Esmerelda is offline Offline
Newbie Poster

first TEN armstrong numbers

 
0
  #1
Sep 17th, 2008
One of the questions given to us for the lab exam was to generate the first 10 armstrong numbers. I could generate upto 5 numbers correctly..the rest are wrong.
  1. #include<stdio.h>
  2. #include<conio.h>
  3.  
  4. void main()
  5. {
  6. int n=1,a,b,i=1,r;
  7. clrscr();
  8. while(i<=10)
  9. {
  10. a=0;
  11. b=n;
  12. do
  13. {
  14. r=b%10;
  15. a=a+r*r*r;
  16. b=b/10;
  17. }while(b>0);
  18. if(a==n)
  19. {
  20. printf("\t%d",n);
  21. i++;
  22. n++;
  23. }
  24. else
  25. n++;
  26. }
  27. getch();
  28. }

the output i got is :
1 153 370 371 407 -729 -216 -125 -64 -1

What should I do to get the correct output?
Last edited by Esmerelda; Sep 17th, 2008 at 1:32 pm.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 11
Reputation: Esmerelda is an unknown quantity at this point 
Solved Threads: 0
Esmerelda's Avatar
Esmerelda Esmerelda is offline Offline
Newbie Poster

Re: first TEN armstrong numbers

 
0
  #2
Sep 17th, 2008
Is it possibe to generate ten armstrong numbers?? I could not even find what the 6th armstrong number is?
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 464
Reputation: invisal is a jewel in the rough invisal is a jewel in the rough invisal is a jewel in the rough 
Solved Threads: 49
invisal's Avatar
invisal invisal is offline Offline
Posting Pro in Training

Re: first TEN armstrong numbers

 
0
  #3
Sep 17th, 2008
An n-digit number that is the sum of the nth powers of its digits is called an n-narcissistic number. It is also sometimes known as an Armstrong number, perfect digital invariant (Wolfram MathWorld)


To me, the reason that you fail to produce those numbers is because you have defined the Armstrong Number incorrectly.
Yesterday is a history, tomorrow is a mystery, today is a gift.
Behind every smile is a tear.
Visal .In
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 11
Reputation: Esmerelda is an unknown quantity at this point 
Solved Threads: 0
Esmerelda's Avatar
Esmerelda Esmerelda is offline Offline
Newbie Poster

Re: first TEN armstrong numbers

 
0
  #4
Sep 17th, 2008
i did it that way 'cos thats how my teacher explained...and when i searched for Armstrong numbers in Daniweb..I found this link posted by Ancient Dragon.
http://www.cs.mtu.edu/~shene/COURSES...ap04/arms.html
It says "An Armstrong number is a number such that the sum
of its digits raised to the third power is equal to the number
itself."

Anyway Thanks..I'll try it this way..
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 464
Reputation: invisal is a jewel in the rough invisal is a jewel in the rough invisal is a jewel in the rough 
Solved Threads: 49
invisal's Avatar
invisal invisal is offline Offline
Posting Pro in Training

Re: first TEN armstrong numbers

 
0
  #5
Sep 17th, 2008
If an Armstrong number is a number such that the sum of its digits raised to the third power is equal to the number itself, then I am sure that there are only 5 Armstrong numbers that does exist in this world. Practically, when the number of digit grow, the sum of raising to the third power of each digit won't able to produce larger enough result to be equal to the number.
For example:
99999 = 9^3 + 9^3 + 9^3 + 9^3 + 9^3 = 3645.
The above example has proved that even we have the largest number in each digit we still cannot produce larger enough result to be equal to its number.
Yesterday is a history, tomorrow is a mystery, today is a gift.
Behind every smile is a tear.
Visal .In
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: first TEN armstrong numbers

 
0
  #6
Sep 17th, 2008
From http://homepages.cwi.nl/~dik/english...rmstrong.html:
An Armstrong number is an n-digit base b number such that the sum of its (base b) digits raised to the power n is the number itself.
a = a^1 { 1, 2, 3, 4, 5, 6, 7, 8, 9 }
ab = a^2 + b^1 {}
abc = a^3 + b^3 + c^3 { 153, 370, 371, 407 }
abcd = a^4 + b^4 + c^4 + d^4 { 1634, 8208, 9474 }
and so on...
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 11
Reputation: Esmerelda is an unknown quantity at this point 
Solved Threads: 0
Esmerelda's Avatar
Esmerelda Esmerelda is offline Offline
Newbie Poster

Re: first TEN armstrong numbers

 
0
  #7
Sep 18th, 2008
  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<math.h>
  4.  
  5. void main()
  6. {
  7. int n1=1,a,n2,i=1,r,dig=0;
  8. clrscr();
  9. while(i<=10)
  10. {
  11. a=0;
  12. n2=n1;
  13. do
  14. {
  15. n2=n2/10;
  16. dig++;
  17. }while(n2>0);
  18. n2=n1;
  19. while(n2>0)
  20. {
  21. r=n2%10;
  22. a=a+pow((double)r,(double)dig);
  23. n2=n2/10;
  24. }
  25. if(a==n1)
  26. {
  27. printf("\n%d",n1);
  28. i++;
  29. n1++;
  30. }
  31. else
  32. n1++;
  33. }
  34. getch();
  35. }

The program is not even running..
Can somebody tell me where I went wrong?
Last edited by Esmerelda; Sep 18th, 2008 at 1:26 am.
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 464
Reputation: invisal is a jewel in the rough invisal is a jewel in the rough invisal is a jewel in the rough 
Solved Threads: 49
invisal's Avatar
invisal invisal is offline Offline
Posting Pro in Training

Re: first TEN armstrong numbers

 
0
  #8
Sep 18th, 2008
The problem that your code doesn't run properly is because you keep increase the dig without reseting it. To solve this, you can simply reset the dig to 0 in the beginning of the loop.

  1. while(i<=10) {
  2. a=0;
  3. dig=0; // reset it to 0
  4. n2=n1;
  5.  
  6. do {
  7. n2 = n2 / 10;
  8. dig++;
  9. } while (n2 > 0);
  10. ....
  11. ....
  12. }

One more thing, next time, you should try to choose better name for your variable. It is idea good to name your variable that could express its purpose. To be honest, your code is very hard to read since there are n1, n2, a, b which look so similar to each other.
Yesterday is a history, tomorrow is a mystery, today is a gift.
Behind every smile is a tear.
Visal .In
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: first TEN armstrong numbers

 
0
  #9
Sep 18th, 2008
In addition to invisal's post:
Don't compute p raised to the power dig with floating point arithmetic pow function. It's too expensive. Better write your own simple integer pow function, for example:
  1. int ipow(int k, int n)
  2. {
  3. int kn = k;
  4. while (n-->1)
  5. kn *= k;
  6. return kn;
  7. }
It's far from the fastest method but it's better than pow(). Think about using table (array) of powers (there are only ten digits for base 10 ).

Think about better condition for the last loop. No need to continue calculations if the partial sum of powers is greater than the number (it's not one of Armstrong numbers).

Look at for loops in C textbook: your while loops are for loops simulations.

Use "%d\n", not "\n%d" format to print the next number.
Last edited by ArkM; Sep 18th, 2008 at 12:00 pm.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 11
Reputation: Esmerelda is an unknown quantity at this point 
Solved Threads: 0
Esmerelda's Avatar
Esmerelda Esmerelda is offline Offline
Newbie Poster

Re: first TEN armstrong numbers

 
0
  #10
Sep 19th, 2008
Thank you everybody..for your help and suggestions..
Last edited by Esmerelda; Sep 19th, 2008 at 1:40 pm.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 1831 | Replies: 10
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC