C Program to find Armstrong numbers
Write a C Program to display Armstrong numbers between Limits (Range).This C Program displays all the armstrong numbers between the range entered by the user.
Armstrong number is a number which is equal to sum of digits raise to the power total number of digits in the number. Some Armstrong numbers are: 0, 1, 2, 3, 153, 370, 407, 1634, 8208 etc. We will consider base 10 numbers in our program.
Algorithm to display Armstrong number is: First we calculate number of digits in our program and then compute sum of individual digits raise to the power number of digits. If this sum equals input number then number is Armstrong and we display the output on the screen otherwise skip the number and proceed to next number.
Hence 153 because 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153.
Here is source code of the C Program to display Armstrong numbers between Limits (Range). The C program is successfully compiled and run(Codeblocks) on a Windows system. The program output is also shown below.
SOURCE CODE : :
#include<stdio.h> #include<math.h> int main() { long int lim_up,n,dig,sum,num,power=0; printf("\n\nENTER THE UPPER LIMIT----- : "); scanf("%d",&lim_up); printf("\n\nARMSTRONG NUMBERS ARE: "); for(n=10;n<lim_up;n++) { num=n; power=0; while (num != 0) { num/= 10; ++power; } sum = 0; num = n; while(num>0) { dig = num%10; sum = sum+pow(dig,power); num = num/10; } if(sum == n) printf("\n\n\t%d",n); } return 0; }
OUTPUT : :
ENTER THE UPPER LIMIT----- : 100000 ARMSTRONG NUMBERS ARE: 153 370 371 407 1634 8208 9474 54748 92727 93084