C program to find armstrong number

What is Armstrong number?

Sum of a number’s digits raised to the power total number of digits is armstrong number.

Armstrong numbers example: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407, 1634 etc

Explanation:

3 = 3^1 = 3

153 = 1^3 + 5^3 + 3^3 = 153

Non-Armstrong numbers:

156 = 1^3 + 5^3 + 6^3 . This value is equal to 342. So, 156 is not an armstrong number.

armstrong number
armstrong number

 

Let see the below C Program:

 

#include<stdio.h>

#include<math.h>

void main()

{

int number, a, b, c, sum=0, count=0;

printf(“\n Enter the number”);

scanf(“%d”,&number);

a=number;

b=a;

while(a>0)

{

count++;

a=a/10;

}

while(b>0)

{

c=b%10;

sum=sum+pow(c,count);

b=b/10;

}

printf(“\n sum=%d”,sum);

if(sum==number)

printf(“\n The number is Armstrong”);

else

printf(“\n The number is not Armstrong”);

getch();

}

Leave a Reply