Armstrong number is a number in which sum of the cubes of all digits equals the number.
371 = 3^3 + 7^3 + 1^3
Let no = 371
371 = 3^3 + 7^3 + 1^3
Code
#include<stdio.h>
int main()
{
int sum=0,rem,no,n;
printf("Enter a number ");
scanf("%d",&no);
n = no;
while(n != 0)
{
rem = n % 10;
sum = sum + rem * rem * rem;
n = n/10;
}
if(no == sum)
printf("\nArmstrong number");
else
printf("\nNot a armstrong number");
return 0;
}
O/P
Code Tracing
Let no = 371
rem = 371 % 10 = 1
sum = 0 + 1 * 1 * 1 = 1
n = 371/10 = 37
rem = 37 % 10 = 7
sum = 1 + 7 * 7 * 7 = 344
n = 37/10 = 3
rem = 3 % 10 = 3
sum = 344 + 3 * 3 * 3 = 371
n = 3/10 = 0 STOP
No comments:
Post a Comment