C program To find given Number is Armstrong or Not

Write a C program to find given number is Armstrong or Not.

Armstrong Number Means: Number must and should an integer and sum of the cubes of its individual digits is equal to the number it self. Example:153=(1*1*1)(5*5*5)(3*3*3)

Method 1:Find Armstrong number in c programming by using for loop

#include<stdio.h>
 int main()
{
 int num,sum=0,temp,rem;
 printf("\n enter any value to find given number is Armstrong or not\t");
 scanf("%d",&num);
 for(temp=num;num!=0)
{
rem=num%10;
 sum=sum+(rem*rem*rem);
num=num/10;
 }
if(sum==temp)
 {
printf("/n%d is an armstrong number",temp);
}
else
{
 printf(""/n%d is not an armstrong number",temp);
}
return 0;
}
Output
Enter a Number 132
132 is not an armstrong number

Method 2:Find Armstrong number in c programming by using for while loop

#include<stdio.h>
int main()
 {
int num,sum=0,temp,rem;
printf("\n enter any value to find given number is Armstrong or not\t");
 scanf("%d",&num);
while(num!=0)
{
rem=num%10;
 sum=sum+(rem*rem*rem);
num=num/10;
}
if(sum==temp)
 {
printf("/n%d is an armstrong number",temp);
 }
else
{
printf(""/n%d is not an armstrong number",temp);
}
return 0;
 }
Output
Enter a Number 153
153 is an armstrong number

Post a Comment