In this example, you will learn whether the input entered by the user is an Armstrong number or not in the C program.
Armstrong number is a number that is equal to the sum of cubes of its digits.
For example:
153, 1634 is Armstrong number. because 13+53+33=153
153=1*1*1+5*5*5+3*3*3 for 3 digits
14+64+34+44=1634 for four digit
#include<stdio.h>
int main()
{
int sum=0,copy,n,r;
printf("\nenter the number:");
scanf("%d",&n);
copy=n;
while(copy>0)
{
r=copy%10;
sum=sum+(r*r*r);
copy=copy/10;
}
if(sum==n)
printf("\n%d is armstrong number",n);
else
printf("\n%d is not a armstrong number",n);
return 0;
}
enter the number:153 153 is armstrong number
#include<stdio.h>
#include<math.h>
void main()
{
int i=0,sum=0,copy,n,r;
printf("\nenter the number:");
scanf("%d",&n);
copy=n;
/*it will calculate number of digit */
while(copy>0)
{
copy=copy/10;
i+=1;
}
copy=n;
/*store the sum of power of individual digit in sum variable */
while(copy>0)
{
r=copy%10;
sum+=pow(r,i);
copy=copy/10;
}
if(sum==n)
printf("\n%d is armstrong number",n);
else
printf("\n%d is not a armstrong number",n);
}
enter the number:1634 1634 is armstrong number
n
.n
into the copy
variable for later comparison.n
and store the number of digits in i
variable.sum
.n
is equal to the sum or not.sum
is equal to n
then if statement
will get executed and it will print “It is an Armstrong number”.else
part gets executed and prints “It is not an Armstrong number”.