# How do i design an Algorithm to check if a number is an Armstrong number?

#### shibaji.paul Profile Answers by shibaji.paul

• Apr 20th, 2006

Check Armstrong Number
1.start
2. input n.
3. n1:=n.
4. let s := 0.
5. r := n1%10.
6. s:=s+ r*r*r.
7. n1 :=n1/10.
8. if n1<>0 then goto step 5.
9.  if n=s then
10. print "ARMSTRONG NO"
11. else
12.print "NOT ARMSTRONG NO"
13. end

The above algorithm would check only the 3 digit numbers, if u need it for all digit numbers then u need to request me again. CHAO.

#### vvsureshreddy Profile Answers by vvsureshreddy

• Aug 29th, 2006

Would appreciate the Generic algoirthm for the problem :-)

#### saurabh

• Sep 16th, 2006

could you please send armstrong algo for the all digits

thnking you

saurabh

#### jyoti

• Sep 27th, 2006

simply add a do-while loop to it for (n>0) and it whould wor for all number of digits

jyoti
www.jyoticlub.jaijyoti.com

#### JonBruse Profile Answers by JonBruse

• Nov 14th, 2006

in C#:

public static bool isArmstrong(int intInteger)

{

// Declarations

int Length, Total1, Temp;

string strString = intInteger.ToString();

Total1 = 0;

//get length of number

Length = strString.Length;

//iterate through each digit, get n power, add to sum

for (int i = 0; i <= Length - 1; i++)

{

Temp = Convert.ToInt32(strString.Substring(i, 1).ToString());

Total1 += Convert.ToInt32(System.Math.Pow(Convert.ToDouble(Temp), Convert.ToDouble(Length)));

}

if (Total1 == intInteger)

{

return true;

}

else

{

return false;

}

}

#### Sukumar Paul Profile Answers by Sukumar Paul

• Aug 1st, 2010

First of all I would like to introduce to you with amstrong number.

Lets take an example: 153 which is amstrong number because 153 = (1*1*1) + (5*5*5) + (3*3*3) i.e.
153 = 1^3 + 5^3 + 3^3(where ^ indicates power i.e. 3^3 is 3*3*3)

Now logic behind writing the program to check the amstrong number:

1: initialize the number i.e. num
y <-- num, sum <-- 0, temp <-- 0
2: repeat step untill y !=0
i: calculate y%10 and store it in temp
ii: calculate temp*temp*temp and add it with the previous value of sum
iii: find y <-- y/10
3: check whether sum is equal to num , if so num is amstrong number
4: otherwise num is not an amstrong number

Here is the actual program which implements the above logic and find out whether a number is amstrong number or not:

<-------------------------------------------------------------------------------------------->
#include
<stdio.h>
void main()
{
int num=153;
int y=num,sum=0,temp;
while(y != 0)
{
temp = y%10;
sum = sum+temp*temp*temp;
y = y/10;
}
if(sum == num)
printf("amstrong number");
else
printf("Not amstrong number");
}
<------------------------------------------------------------------------------------>

#### Kapil S Shinde

• Feb 25th, 2014

```Codestart
Accept num

noOfDigit=calculate number of digits   //use counting digit function

while(num!=0)
{
remainder=inum%10;
sum=sum+pow(remainder,noOfDigit);     //use power function from math.h header file
num=num/10;
}
if(sum==num)
return true
else
return false
stop```

#### jbode Profile Answers by jbode

• Mar 7th, 2014

First, count up the number of digits in the candidate value; an easy way to do this is take the floor value of the log of the number and add 1 (for example, floor(log10(1234)) = 3, so 3 + 1 = 4 digits).

Then extract each digit (an easy way to do this is to use a remainder or modulus operation), raise it to the power of the number of digits, and sum the results together.

Compare this sum to your candidate value; if they're equal, then the candidate value is narcissistic. Attached is an example written in C that examines a single number or range of numbers using base 10, along with some sample output.

```Code/**
* This program analyzes a range of numbers to see if they are
* narcissistic or Armstrong numbers (sum of its own digits, each
* raised to the power of the number of digits) in base 10.
*
* The output is simply the number if it meets the necessary
* criteria.
*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>

/**
* Test a candidate value to see if it is narcissisitc.
* Returns true (1) or false (0).
*/
int test( long cand )
{
int digits = (int) log10( (double) cand ) + 1;
long sum = 0;
long temp = cand;

for ( int i = 0; i < digits; i++ )
{
int curdigit = temp % 10;
temp /= 10;
sum += (long) pow( (double) curdigit, digits );
}

return sum == cand;
}

/**
* Process the command line arguments; must provide
* a valid integer or range of integers.
*/
int getargs( int argc, char **argv, long *start, long *end )
{
int result = 1;
if ( argc < 2 )
{
result = 0;
}
else if ( argc >= 2 )
{
char *chk;
long val = strtol( argv[1], &chk, 0 );
if ( isspace( *chk ) || *chk == 0 )
*start = *end = val;
else
result = 0;

if ( argc > 2 )
{
val = (int) strtol( argv[2], &chk, 0 );
if ( isspace( *chk ) || *chk == 0 )
*end = val;
else
result = 0;
}
}
return result;
}

/**
* Main program; loop through the specified range
* of values and test each for narcissism; if
* the number is narcissistic, write it to
* standard output.
*/
int main( int argc, char **argv )
{
long start=0, end=0;
const char *usage = "USAGE: %s start [end]n";
if ( !getargs (argc, argv, &start, &end ))
{
fprintf( stderr, usage, argv[0] );
return EXIT_FAILURE;
}

for( long t = start; t <= end; t++ )
if ( test( t ))
fprintf( stdout, "%ld
", t );

return EXIT_SUCCESS;
}

[fbgo448@n9dvap997]~/prototypes/armstrong: gcc -o armstrong -std=c99 -pedantic -g -Wall -Werror armstrong.c -lm
[fbgo448@n9dvap997]~/prototypes/armstrong: ./armstrong 153
153
[fbgo448@n9dvap997]~/prototypes/armstrong: ./armstrong 154
[fbgo448@n9dvap997]~/prototypes/armstrong: ./armstrong 0 10000
0
1
2
3
4
5
6
7
8
9
153
370
371
407
1634
8208
9474
```