# Thread: Printing Armstrong Numbers from 1-500

1. ## Printing Armstrong Numbers from 1-500

Code:
```/*
Write a program to print out all Armstrong numbers between 1 and 500.
If sum of cubes of each digit of the number is equal to the number itself,
then the number is called an Armstrong number.
For example,
153 = (1*1*1) + (5*5*5) + (3*3*3)
*/

#include<stdio.h>
main()
{
int number, temp, digit1, digit2, digit3;

printf("Printing all Armstrong numbers between 1 and 500:\n\n");

number = 001;

while (number <= 500)
{
digit1 = number%10;
digit2 = (number%100) - ((number/100)*10);
digit3 = number%1000;

temp = (digit1*digit1*digit1) + (digit2*digit2*digit2) + (digit3*digit3*digit3);

if (temp == number)
{
printf("\nAmstrong Number:%d", temp);
}

number++;
}
}```

Whats wrong with this code??

It does not give any output at all! Use a series of /10 and %10 3. Okay..i will try that.. 4. Also try printing out your digits (or examine them with the debugger) as you go. 5. Worked I changed the digit extraction scheme to this:

Code:
```digit1 = number - ((number / 10) * 10);
digit2 = (number / 10) - ((number / 100) * 10);
digit3 = (number / 100) - ((number / 1000) * 10);``` 6. But still...

number%10 --- should extract the last number
and
number%100 -- should have extracted the first number (of a three digit number), isn't it? 7. 123 % 100 gets you 23
23 / 10 gets you 2 Popular pages Recent additions 