In this example, we take on input from user and check entered number is Armstrong or not.
In number theory, a Armstrong number in a given number base b is a number that is the sum of its own digits each raised to the power of the number of digits.
For example, using a simple number 153 and the decimal system, we see there are 3 digits in it. If we do a simple mathematical operation of raising each of its digits to the power of 3, and then obtain total, we get 153. That is 1 to the power of 3 5 to the power of 3 3 to the power of three is 1 125 27 153. This can also be represented as 1^3 5^3 3^3=153. The number 153 is an example of the Armstrong number which also has a unique property that one can use any number system.
#include<stdio.h>
int main()
{
int n,r,sum=0,temp;
printf("Enter the number ::");
scanf("%d",&n);
temp=n;
while(n>0)
{
r=n%10;
sum=sum+(r*r*r);
n=n/10;
}
if(temp==sum)
printf("Number is Armstrong Number ");
else
printf("Number is not Armstrong Number");
return 0;
}
Output :
//Output : 1
Enter the number ::153
Number is Armstrong Number
//Output : 2
Enter the number ::122
Number is not Armstrong Number
#include<stdio.h>
int isArmstrong(int number)
{
int lastDigit = 0;
int power = 0;
int sum = 0;
int n = number;
while(n!=0) {
lastDigit = n % 10;
power = lastDigit*lastDigit*lastDigit;
sum += power;
n /= 10;
}
if(sum == number) return 0;
else return 1;
}
int main()
{
int number;
printf("Enter number: ");
scanf("%d",&number);
if(isArmstrong(number) == 0)
printf("%d is an Armstrong number.\n", number);
else
printf("%d is not an Armstrong number.", number);
return 0;
}
Output :
Enter number: 8526
8526 is not an Armstrong number.
#include<stdio.h>
#include<math.h>
int is_armstrong(int num)
{
if(num>0)
return (pow(num%10,3) +is_armstrong(num/10));
}
int main()
{
int num;
printf("Enter a number:");
scanf("%d",&num);
if(is_armstrong(num)==num)
printf("It is an Armstrong Number");
else
printf("It is not an Armstrong Number");
}
Output :
Enter a number:370
It is an Armstrong Number
#include<stdio.h>
int main()
{
int start,end,r,sum=0,i,n;
printf("Enter the starting point ::");
scanf("%d",&start);
printf("Enter the starting point ::");
scanf("%d",&
end);
printf("Armstrong numbers between %d and %d are ::",start,end);
for(i=start;i<=end;i++)
{
n=i;
sum = 0;
while(n>0)
{
r=n%10;
sum=sum+(r*r*r);
n=n/10;
}
if(i==sum)
{
printf(" %d ",i);
}
}
return 0;
}
Output :
Enter the starting point ::50
Enter the starting point ::500
Armstrong numbers between 50 and 500 are :: 153 370 371 407
Ask anything about this examples