Article by: Manish Methani
Last Updated: September 26, 2021 at 2:04pm IST
An Armstrong number is a number that equals the sum of the cube of each digit in a number.
For example, suppose a given number is 153.
To check whether a given number is Armstrong or not, we have to take the cube of each digit in a number and take a sum of those.
153 = (1*1*1) + (5*5*5) + (3*3*3)
After the addition of the cube of each digit, we get the original number which is 153. Hence, the given number is Armstrong.
#include <stdio.h> int main() { int n = 153, sum = 0, remainder = 0,tempNumber; tempNumber = n; while(tempNumber!=0) { remainder = tempNumber % 10; sum = sum + (remainder*remainder*remainder); tempNumber /= 10; } if(sum==n) { printf("%d is an Armstrong number.",n); } else { printf("%d is not an Armstrong number.",n); } return 0; }
153 is an Armstrong number
If at the end we find that this new number after addition is found as. same or not. If yes, then the given number is an Armstrong number else it is not an Armstrong number.
#include <stdio.h> int isArmstrong(int n) { int tempNumber=n; int remainder,sum; sum=0; while(tempNumber!=0) { remainder=tempNumber%10; sum = sum + (remainder*remainder*remainder); tempNumber/=10; } if(sum==n) return 1 else return 0; } int main() { int n = 153; if(isArmstrong(n)) printf("%d is an Armstrong number.",n); else printf("%d is not an Armstrong number.",n); return 0; }
153 is an Armstrong number