ARMSTRONG NUMBER PROGRAM IN C
In this example, you will learn a program to check whether given number is Armstrong number or not.
Armstrong Number:
If sum of cubes of digits of a number is equal to the original number itself, is called as Armstrong Number.
Example:
consider the number 153,
Let's calculate the sum of cubes of its all digits.
i.e. 1 + 125 + 27 = 153, and this is equal to the original n umber(153) itself.
Thus. 153 is Armstrong number .
Program to check whether a given number is Armstrong number or not:
//check whether given number is prime or not
#include<stdio.h>
int main( )
{
int num, r, sum=0, temp=0;
printf("Enter the number :");
scanf("%d",&num);
temp=num;
while(num!=0)
{
r=num%10; // to get last digit of number
sum=sum + r*r*r
num= num/10;
}
if(temp==sum)
printf("%d is Armstrong number",temp);
else
printf("%d is not Armstrong number",temp);
return 0;
}
#include<stdio.h>
int main( )
{
int num, r, sum=0, temp=0;
printf("Enter the number :");
scanf("%d",&num);
temp=num;
while(num!=0)
{
r=num%10; // to get last digit of number
sum=sum + r*r*r
num= num/10;
}
if(temp==sum)
printf("%d is Armstrong number",temp);
else
printf("%d is not Armstrong number",temp);
return 0;
}
Output:
Enter the number:153
153 is Armstrong number
Enter the number:162
162 is not Armstrong number
153 is Armstrong number
Enter the number:162
162 is not Armstrong number
Try this by your own!
If you have any doubts feel free to drop a comment. I'll be happy to answer your questions in the comment section.
Keep coding...
Comments
Post a Comment