I want to know how to write a program to print all Armstrong numbers between 1 and 500.

He's referring to LANCE Armstrong, of course, and the numbers are:

1
1
1
1
1
1

First 6 times in a row!

Start out simple. Write a function that iterates through numbers between 0 and 500, and does the sum the cubes test. Usually we just call this type of algorithm a search over parameter space. For all sorts of problems for which we have no answers, we simply search for it.

for (hundreds_digit from 0 to 5)
   for (tens_digit from 0 to 9)
      for (ones digit from 0 to 9)
          [B]if (sum the digits^3) == number[/B]
                  print number

- or - more generally for an arbitrary base:

You could do this on Decimal (base 10) or even Hex (base 16)

for (hundreds_digit from 0 to 5)
   for (tens_digit from 0 to Base-1)
      for (ones digit from 0 to Base-1)
          // be careful about the 'sum the digits^3'
          // make sure you know what base you are in.
          // you may want to convert the number to decimal first
          [B]if (sum the digits^3) == number[/B]
                  print number

Armstrong numbers aren't a solved problem, so you aren't going to find a closed-form formula to generate the next number. You can probably find a more clever way to perform the search over parameter space of numbers. I highly encourage you to try to make a more efficient program; it'll train your critcal thinking skills.


Ed

The program in C is(for 1-500)

#include<conio.h>
#include<math.h>
#include<stdio.h>
main()
{
int a,b,c,d,i;
for(i=500;i>=1;i--)
{
a=(i-(i%100))/100;
b=((i%100)-(i%10))/10;
c=(i%10);
d= (a*a*a)+(b*b*b)+(c*c*c);
if(d==i)
printf("%d\n",i);
}
getch();
}
:mrgreen:

This article has been dead for over six months. Start a new discussion instead.