Solution:
- A positive integer is called an Armstrong number (of order n) if xyz… = x^n + y^n + z^n
- In the case of an Armstrong number of 3 digits, the sum of cubes of each digit is equal to the number itself.
- For example, 153 is an Armstrong number because 153 = 1*1*1 + 5*5*5 + 3*3*3
#include<stdio.h>
int main()
{
int n,r,sum=0,temp;
printf("Enter n value : ");
scanf("%d",&n);
temp=n;
while(n>0)
{
r=n%10;
sum=sum+(r*r*r);
n=n/10;
}
n=temp;
if(temp==sum)
printf("%d is Armstrong number \n", n);
else
printf("%d is Not an armstrong number\n", n);
return 0;
}
Approach-2:
- In the first approach, we check only 3 digits number is strong number or not.
- In the following approach, we can mention any number of digits to check whether Armstrong or not.
- In the code, we write custom function to check the order of number as well as to find the power of input number.
#include <stdio.h>
int power(int, int);
int order(int);
int main()
{
int n=153, sum=0, temp, r;
int d = digits(n);
printf("Input number is : %d \n", n);
printf("Digits : %d\n", d);
temp=n;
while(n)
{
r = n%10;
sum += power(r, d);
n = n/10;
}
n=temp;
if(n==sum)
printf("Armstrong number\n");
else
printf("Not an ArmStrong number\n");
return 0;
}
int digits(int x)
{
int d = 0;
while(x)
{
d++;
x=x/10;
}
return d;
}
int power(int x, int y)
{
int i, res=x;
for(i=1 ; i<y ; i++)
{
res = res*x;
}
return res;
}