Monday, February 16, 2009

Write a C program to detect whether the given integer is Armstrong number or not.

Armstrong number:- Sum of the cubes of a individual number is known as Armstrong number.

Example:- Suppose take 153,
153= 1 + 125 + 27
ie., 153 = 1 cube + 5 cube + 3 cube
153 = 153, Then we say '153' is Armstrong number

#include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(conio.h)
void main( )
{
int rem, sum=0, n, temp;
clrscr( );
printf("Enter the required number:");
scanf("%d",&n);
temp= n;
while(n>0)
{
rem=n%10;
sum = sum + (rem * rem * rem);
n=n/10;
}
if(temp == sum)
printf("\nArmstrong Number.");
else
printf("\nNot a Armstrong number.");
getch( );
}

Output:-
Enter the required number: 153
Armstrong Number.

Note:- If u have any doubt regarding this program or logic, please feel free to contact me.

No comments:

Post a Comment