0
10kviews
Write a program to display Armstrong numbers between 1 and 1000.
1 Answer
0
585views

A number is said to be Armstrong number if the sum of the cube of its digits is equal to the number itself. For e.g. 153, the sum of the cubes is 13 + 53 + 33 = 1 + 125 + 27 = 153. i.e. the original number itself.

Algorithm:

  • Start.

  • Enter the range of numbers.

  • Input n.

  • X = 99.

  • If n = 0, then goto step 14.

  • X = X + 1, copy = x, sum = 0.

  • If X = 0, then goto step 9.

  • Digit = X mod 10.

    Sum = sum + digit * digit * digit.

  • X = X / 10.

  • Goto step 7.

  • If sum = copy then Print “Armstrong number is” and n = n - 1.

  • X = copy.

  • Goto step 5

  • Stop.

Program:

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

printf("Print all Armstrong numbers between 1 and 1000:\n");
number = 001;
while (number <= 999)    {
digit1 = number - ((number / 10) * 10);
      digit2 = (number / 10) - ((number / 100) * 10);
digit3 = (number / 100) - ((number / 1000) * 10);
temp = (digit1 * digit1 * digit1) + (digit2 * digit2 * digit2) + (digit3 * digit3 * digit3);
if (temp == number)  {
printf("\n Armstrong no is:%d", temp);
}

number++;       }
}

Output:

Print all Armstrong numbers between 1 and 1000:

Armstrong no is: 1

Armstrong no is: 153

Armstrong no is: 370

Armstrong no is: 371

Armstrong no is: 407

Please log in to add an answer.