Write a C program given number Armstrong number or not

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

#include<stdio.h>
int main(){
int num,a,p,total=0;
printf("Enter a number:");
scanf("%d",&num);
a=num;
while(a!=0){
p=a%10;
total=total+(p*p*p);
a=a/10;
}
if(total == num){
printf("%d is a Armstrong number.\n",num);
}else{
printf("%d is not a Armstorng number.\n",num);
}
return 0;
}

Output:

Enter a number:371
371 is a Armstrong number.

 

For N digits:

Program:

#include <stdio.h>
#include <math.h>
int main()
{
int number, a, p, total = 0, b = 0 ;
printf("Enter an integer: ");
scanf("%d", &number);
a = number;
while (a != 0)
{
a =a/10;
++b;
}
a = number;
while (a != 0)
{
p = a%10;
total = total + pow(p, b);
a=a/10;
}
if(total==number){
printf("%d is an Armstrong number.\n",number);
}else{
printf("%d is not an Armstorng number.\n",number);
}
return 0;
}
Output:

Enter an integer: 8208
8208 is an Armstrong number.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top