Program to check whether a number is Armstrong Number or not:
Armstrong number is a positive number whose sum of cubes of all individual digits is equal to the number itself. Like 371(3x3x3+7x7x7+1x1x1=371) is an armstrong number as the sum of cubes of all individual digits is equal to 371.
In this program user will enter a number and then we will iterate the while loop until number does not become zero. Then we will compare number variable value with the value entered by the user if they match "if" block will execute otherwise else block will execute.
#include <stdio.h>
int main()
{
int n, num, r, number=0;
printf("Enter a positive number: ");
scanf("%d", &n);
num=n; // assigning n variable value to variable num
while(num!=0) // It will iterate until num does not become equal to 0
{
r=num%10;
number+=r*r*r; //calculate cube of remainder and will sum the value in number variable
num/=10;
}
if(number==n)
printf("You entered an Armstrong number");
else
printf(" Not an Armstrong number");
}
Output 1(If a user enter armstrong number):
Enter a positive number: 153
You entered an Armstrong number
Output 2 (If a user enter a number which is not a armstrong number):
Enter a positive number: 121
Not an Armstrong number
1 Comment(s)