Tuesday, April 30, 2013

c Program to Check Whether the Given Number is an Armstrong Number


/*
Program to Check Whether the Given Number is an Armstrong Number
*/
#include <stdio.h>
main()
{
int n, temp, d, arm=0;
printf("\nEnter any number: ");
scanf("%d", &n);
temp = n;
while (temp > 0)
{
d = temp%10;
temp = temp/10;
arm = arm + (d*d*d);
}
if (arm == n)
printf("\n\n%d is an Armstrong number\n", n);
else
printf("\n\n%d is not an Armstrong number\n", n);
getch();
}


An Armstrong number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself. For example, 371 is an Armstrong number since 3^3 + 7^3 + 1^3 = 371.
Write a program to find all Armstrong number in the range of 0 and 999.

For finding whether a number is Armstrong or not we must follow the following steps

step 1:  take each digit separately from the   right side. Using modular division with 10 satisfies our need..

d = temp%10;
step 2: 
 temp = temp/10; This step makes a number to reduce it's width from right side ie 371 to 37
step3:
Don't forgot to set the value of arm to 0.
(d*d*d) it produces the cube of the number.
arm = arm + (d*d*d);while checking this number to the original number we will get the answer.

No comments:

Post a Comment