Program for Armstrong Number

Started by thiruvasagamani, Sep 22, 2008, 11:30 AM

Previous topic - Next topic

thiruvasagamani

Test a number is armstrong or not

#include <stdio.h>
#include <conio.h>

int armstrong( int n)
{
int sum = 0, a, rem;
a= n; // copy given value to a different variable 'a'
while( n > 0) // execute loop until 'n' becomes 0
{
rem = n % 10; // extract digits in number from right to left
sum + = rem * rem * rem ; // cube each digit is number and add to sum
n = n/10; // modify 'n' value to avoid infinite loop
}
return ( a == sum); // sum of the cubes of digit equal to given number
}

main()
{
int n, armstrong ( n);
printf( " enter any value \n");
sacnf("%d",&n);
if( armstrong ( n) )
printf( " armstrong number \n");
else
printf(" not armstrong number \n");
getch();
}


o/p:
If enter a number 'n' is 153 output is armstrong number

NOTE :
If you enter 'n' is 153 sum of cubes of the each digit in a number is equal to a given number
If n= 153
sum = 13 + 53 + 33 = 153 it is armstrong number
Thiruvasakamani Karnan