Armstrong Numbers are those numbers which are equal to the sum of its own digits each raised to the power of the number of digits in the number. Consider number 153, it has three digits, so length is 3.
Now each digits raised to the power of the number of digits in the number : 13 = 1, 53 = 125, 33 = 27.
Sum of these values : 1 + 125 + 27 = 153. So, sum is equal to the number itself, so 153 is an Armstrong Number.
Armstrong Numbers are also known as Narcissistic Numbers, Pluperfect Digital Invariant (PPDI) and Plus Perfect Numbers. You can learn more about Armstrong Numbers on Wikipedia.
Algorithm:
- Let
numis the number which is to be checked whether it’s an Armstrong Number or not. - Let
af(additive factor),sumandtempbe three temporary variables,sumhaving value 0 andtemphaving value equal to variablenuminitially. - Check if
numis less than zero or not, if yes, print an error message that only positive numbers are allowed and exit the program. Otherwise continue to next step. - Find the length of the number i.e. how many digits it has and store it in variable
length. - Now check if
tempis equal to zero or not. If yes, proceed to 9th step, else continue to next step. - Fetch rightmost digit of
tempand store it in variableaf. - Calculate
aflengthi.e. digit raised to power of the number of digits in number and store inafitself. - Add
aftosumand remove rightmost digit fromtempvariable by dividing it with 10 and move to step 5. - Check if variable
sumis equal to actual numbernum. If yes, thennumis an Armstrong Number, else it is not. - Quit the program.
Instructions:
- We only need a single header file for this program to work.
#include <stdio.h>
- Now in
mainfunction ask the user to enter the number he/she wants to check. Check if it is positive number or not. If not, then exit the program.
int num, temp; printf("Enter the number which you want to check : "); scanf("%d", &num); if(num < 0) { printf("Number entered is not valid positive number"); return 0; } - Now call user defined function
isArmstrong(explained in next step) to calculate sum of its digits each raised to power of number of digits in the number. After than check if value returned byisArmstrongfunction is equal to actual number or not. If yes, then number entered by user is Armstrong Number, otherwise it is not. Print desired message and quit the program.
temp = isArmstrong(num); if(temp == num) printf("%d is an Armstrong number...", num); else printf("%d is not an Armstrong number...", num); - In
isArmstrongfunction, find sum of its digits each raised to power of number of digits in the number using aforloop. Before that we have to find number of digits in the number as well. Finally return the sum calculated.
int isArmstrong(int num) { int temp, temp2, af, length = 0, sum = 0; temp = num; while(temp) { length++; temp /= 10; } for(temp = num; temp; temp /= 10) { af = temp % 10; for(temp2 = 1; temp2 < length; temp2++) af *= temp % 10; sum += af; } return sum; }
Here are some screenshots of the working sample program.
You can download the sample program as well from here.
Incoming search terms:
- how to add id tag to notepad (2)
- algorithm for armstrong number in unix (1)
- greatest common factor of 1784 (1)



