- Write a C program to generate armstrong numbers.

A number is called an **Armstrong number** if the sum of cubes of every digit is equal to the number itself. Given a number N, we have to generate a list of all Armstrong numbers between 0 and N.

*For Example*

407 is an **Armstrong number**

407 = 4*4*4 + 0*0*0 + 7*7*7

121 is not an

**Armstrong number**

121 is not equal to 1*1*1 + 2*2*2 + 1*1*1

Examples of Armstrong Numbers : 0, 1, 2, 3, 153, 370, 407 etc.

**Algorithm to Generate Armstrong number**

- Take a number as input from user and store it in number variable.
- Then using for loop we iterate from 0 till N using a counter variable.
- Find the cubic sum of digits of counter, and store it in sum variable.
- Compare counter and sum.
- If both are equal then current number(counter) is Armstrong number otherwise not an Armstrong number.

## C program to generate armstrong numbers between 0 and N

This program first takes a number as input from user using scanf function and stores it in variable 'number'. Then using a for loop, it perform armstrong number check for every number from 0 to 'number'. It calculates the cube of every digit of counter using getCubicSumOfDigits function and stores in a 'sum' variable. If sum is equal to number then it is an Armstrong number otherwise not an Armstrong number.

/* * C Program to generate armstrong number */ #include <stdio.h> #include <conio.h> int getCubicSumOfDigits(int number); int main(){ int number, sum, counter; printf("Enter a number : "); scanf("%d", &number); printf("Armstrong numbers between 0 and %d\n", number); /* Iterate from 0 till N, and check for Armstrong number */ for(counter = 0; counter <= number; counter++){ sum = getCubicSumOfDigits(counter); if(sum == counter){ printf("%d\n", counter); } } getch(); return 0; } /* * Funtion to calculate the sum of cubes of digits of a number * getCubicSumOfDigits(123) = 1*1*1 + 2*2*2 + 3*3*3; */ int getCubicSumOfDigits(int number){ int lastDigit, sum = 0; while(number != 0){ lastDigit = number%10; sum = sum + lastDigit*lastDigit*lastDigit; number = number/10; } return sum; }

**Program Output**

Enter a number : 10000 Armstrong numbers between 0 and 10000 0 1 153 370 371 407

**Related Topics**