Find The Number Is Armstrong Or Not
Introduction:
An Armstrong number is an n-digit number that is equal to the sum of the nth powers of its digits.
For example, 153 is an Armstrong number because a cube of 1 is 1(1x1x1=1) + a cube of 5 is 125(5*5*5=125) + a cube of 3 is 27(3*3*3=27). Now add all the cubes 1+125+27=153 which equal to number itself.
Algorithm of Armstrong Number:
Step 1: START
Step 2: [INITIALIZE] SUM=0, TEMP=0
Step 3: READ N
Step 4: SET TEMP=N
Step 5: REPEAT Step 5 to 8 WHILE N!=0
Step 6: SET REM=N%10
Step 7: SET SUM=SUM+(REM*REM*REM)
Step 8: SET N=N/10
Step 9: IF SUM==TEMP THEN GO TO Step 10
ELSE
PRINT NOT ARMSTRONG NUMBER
Step 10: PRINT ARMSTRONG NUMBER
Step 11: STOP
Program to Find THE NUMBER IS ARMSTRONG OR NOT:
#include<conio.h>
main()
{
int rem,sum=0,temp=0,n;
printf("\n Enter The Number");
scanf("%d",&n);
temp=n;
while(n!=0)
{
rem=n%10;
sum=sum+(rem*rem*rem);
n=n/10;
}
if(sum==temp)
{
printf("\n The Number is Amstrong Number");
}
else
{
printf("\n The Number is not Amstrong Number");
}
getch();
}
Output of Armstrong Number:
Here is the output generated by the program-
1st case:
2nd case:
3rd case:
Discussion:
Three-digit numbers are 000, 001, 002, ..., 009, 010, 011, ..., 019, 020, 021, 022, ..., 099, 100, 101, 102, ..., 109, 110, ... 990, 991, ..., 999. As you can see the right-most digits changes faster than the middle one, which in turn is faster than the left-most one.
As the left-most and the middle digits are fixed to 0 and 0, the right-most digit changes from 0 to 9. Then, the middle one is increased from 0 to 1.
In other words, whenever the right-most digit completes a 0 to 9 cycle, the middle digit is increased by one and the right-most digit restart another 0 to 9 cycle. By the same token, whenever the middle digit completes a 0 to 9 cycle, the left-most digit is increased by 1 and the middle digit restarts another 0 to 9 cycle.
0 Comments