| | |
first TEN armstrong numbers
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
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.
the output i got is :
1 153 370 371 407 -729 -216 -125 -64 -1
What should I do to get the correct output?
C Syntax (Toggle Plain Text)
#include<stdio.h> #include<conio.h> void main() { int n=1,a,b,i=1,r; clrscr(); while(i<=10) { a=0; b=n; do { r=b%10; a=a+r*r*r; b=b/10; }while(b>0); if(a==n) { printf("\t%d",n); i++; n++; } else n++; } getch(); }
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.
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
Behind every smile is a tear.
Visal .In
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..
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..
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: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.
99999 = 9^3 + 9^3 + 9^3 + 9^3 + 9^3 = 3645.
Yesterday is a history, tomorrow is a mystery, today is a gift.
Behind every smile is a tear.
Visal .In
Behind every smile is a tear.
Visal .In
From http://homepages.cwi.nl/~dik/english...rmstrong.html:
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...
•
•
•
•
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.
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...
C Syntax (Toggle Plain Text)
#include<stdio.h> #include<conio.h> #include<math.h> void main() { int n1=1,a,n2,i=1,r,dig=0; clrscr(); while(i<=10) { a=0; n2=n1; do { n2=n2/10; dig++; }while(n2>0); n2=n1; while(n2>0) { r=n2%10; a=a+pow((double)r,(double)dig); n2=n2/10; } if(a==n1) { printf("\n%d",n1); i++; n1++; } else n1++; } getch(); }
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.
The problem that your code doesn't run properly is because you keep increase the
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.
dig without reseting it. To solve this, you can simply reset the dig to 0 in the beginning of the loop. C Syntax (Toggle Plain Text)
while(i<=10) { a=0; dig=0; // reset it to 0 n2=n1; do { n2 = n2 / 10; dig++; } while (n2 > 0); .... .... }
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
Behind every smile is a tear.
Visal .In
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:
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.
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:
c Syntax (Toggle Plain Text)
int ipow(int k, int n) { int kn = k; while (n-->1) kn *= k; return kn; }
).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.
![]() |
Other Threads in the C Forum
- Previous Thread: How to color the text in microsoft visual c++?
- Next Thread: how to display a image in c
Views: 1831 | Replies: 10
| Thread Tools | Search this Thread |
Tag cloud for C
#include * append array arrays asterisks binarysearch calculate changingto char character cm command copyimagefile cprogramme creafecopyofanytypeoffileinc database directory dynamic execv feet fgets file fork forloop framework function functions givemetehcodez grade graphics gtkwinlinux hacking histogram homework include incrementoperators input intmain() iso kernel keyboard km lazy license linked linkedlist linux list lists locate logical_drives looping loopinsideloop. lowest matrix microsoft mqqueue mysql number oddnumber odf opensource overwrite owf pdf performance pointer pointers posix problem probleminc process program programming radix recursion recv recvblocked research reversing scanf scripting segmentationfault sequential socket spoonfeeding standard string student systemcall testing threads turboc unix user variable wab whythiscodecausesegmentationfault windowsapi






