Armstrong number programme in C

First we should know what is Armstrong number then we can write Armstrong number programme in C

ARMSTRONG NUMBERS

A number N is an Armstrong number of order n (n being the number of digits)
if

abed. . . = an + bn + en + dn .+ . . . = N

More generally, an n-digit number in base b is said to be a base b Armstrong number of order n if it equals the sum of the nth powers of its base b digits

The number 15 3 is an Armstrong number of order 3 because

l3+ 53 + 33 = 1 + 125 + 27 = 153

Likewise, 54748 is an Armstrong number of order 5 because

53 + 43 + 73+ 43 + 83 = 3125 + 1024 + 16807 + 1024 + 32768 = 54748

Full programme of Armstrong number in C

#include<stdio.h>
int main(){
int n,d,digit,flag=0,sum=0,mul=1,checker;
printf("enter any number to check weather a number is armstrong or not\n");
scanf("%d",&n);
d=n;
checker=n;
if(n<1){
    printf("number should be natural number");
    return 0;
}
while(d>0){
d=d/10;
flag++;
}
while(n>0){
for(int i=0;i<flag;i++){
    mul=mul*(n%10);
}
sum=sum+mul;
mul=1;
n=n/10;
}
if(sum==checker){
printf("number is Armstrong number");
}
else{
printf("number is not armstrong number");
}
return 0;    
}

Leave a Comment