GeekInterview.com
Series: Subject: Topic:
Question: 350 of 589

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

Asked by: Interview Candidate | Asked on: Apr 19th, 2006
Showing Answers 1 - 8 of 8 Answers
shibaji.paul

Answered On : Apr 20th, 2006

View all answers by shibaji.paul

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.

  
Login to rate this answer.
vvsureshreddy

Answered On : Aug 29th, 2006

View all answers by vvsureshreddy

Would appreciate the Generic algoirthm for the problem :-)

  
Login to rate this answer.
saurabh

Answered On : Sep 16th, 2006

could you please send armstrong algo for the all digits

thnking you

saurabh

  
Login to rate this answer.
jyoti

Answered On : 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

  
Login to rate this answer.
JonBruse

Answered On : Nov 14th, 2006

View all answers by JonBruse

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;

}

}

  
Login to rate this answer.
Sukumar Paul

Answered On : Aug 1st, 2010

View all answers by Sukumar Paul

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");
}
<------------------------------------------------------------------------------------>

  
Login to rate this answer.
Kapil S Shinde

Answered On : Feb 25th, 2014

Code
  1. start
  2. Accept num
  3.  
  4. noOfDigit=calculate number of digits   //use counting digit function
  5.  
  6. while(num!=0)
  7. {
  8. remainder=inum%10;
  9. sum=sum+pow(remainder,noOfDigit);     //use power function from math.h header file
  10. num=num/10;
  11. }
  12. if(sum==num)
  13.  return true
  14. else
  15.  return false
  16. stop

  
Login to rate this answer.
jbode

Answered On : Mar 7th, 2014

View all answers by jbode

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
  1. /**
  2.  * This program analyzes a range of numbers to see if they are
  3.  * narcissistic or Armstrong numbers (sum of its own digits, each
  4.  * raised to the power of the number of digits) in base 10.
  5.  *
  6.  * The output is simply the number if it meets the necessary
  7.  * criteria.
  8.  */
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <ctype.h>
  12. #include <math.h>
  13.  
  14. /**
  15.  * Test a candidate value to see if it is narcissisitc.
  16.  * Returns true (1) or false (0).
  17.  */
  18. int test( long cand )
  19. {
  20.   int digits = (int) log10( (double) cand ) + 1;
  21.   long sum = 0;
  22.   long temp = cand;
  23.  
  24.   for ( int i = 0; i < digits; i++ )
  25.   {
  26.     int curdigit = temp % 10;
  27.     temp /= 10;
  28.     sum += (long) pow( (double) curdigit, digits );
  29.   }
  30.  
  31.   return sum == cand;
  32. }
  33.  
  34. /**
  35.  * Process the command line arguments; must provide
  36.  * a valid integer or range of integers.
  37.  */
  38. int getargs( int argc, char **argv, long *start, long *end )
  39. {
  40.   int result = 1;
  41.   if ( argc < 2 )
  42.   {
  43.     result = 0;
  44.   }
  45.   else if ( argc >= 2 )
  46.   {
  47.     char *chk;
  48.     long val = strtol( argv[1], &chk, 0 );
  49.     if ( isspace( *chk ) || *chk == 0 )
  50.       *start = *end = val;
  51.     else
  52.       result = 0;
  53.  
  54.     if ( argc > 2 )
  55.     {
  56.       val = (int) strtol( argv[2], &chk, 0 );
  57.       if ( isspace( *chk ) || *chk == 0 )
  58.         *end = val;
  59.       else
  60.         result = 0;
  61.     }
  62.   }
  63.  return result;
  64. }
  65.  
  66. /**
  67.  * Main program; loop through the specified range
  68.  * of values and test each for narcissism; if
  69.  * the number is narcissistic, write it to
  70.  * standard output.
  71.  */
  72. int main( int argc, char **argv )
  73. {
  74.   long start=0, end=0;
  75.   const char *usage = "USAGE: %s start [end]n";
  76.   if ( !getargs (argc, argv, &start, &end ))
  77.   {
  78.     fprintf( stderr, usage, argv[0] );
  79.     return EXIT_FAILURE;
  80.   }
  81.  
  82.   for( long t = start; t <= end; t++ )
  83.     if ( test( t ))
  84.       fprintf( stdout, "%ld
  85. ", t );
  86.  
  87.   return EXIT_SUCCESS;
  88. }
  89.  
  90. [fbgo448@n9dvap997]~/prototypes/armstrong: gcc -o armstrong -std=c99 -pedantic -g -Wall -Werror armstrong.c -lm
  91. [fbgo448@n9dvap997]~/prototypes/armstrong: ./armstrong 153
  92. 153
  93. [fbgo448@n9dvap997]~/prototypes/armstrong: ./armstrong 154
  94. [fbgo448@n9dvap997]~/prototypes/armstrong: ./armstrong 0 10000
  95. 0
  96. 1
  97. 2
  98. 3
  99. 4
  100. 5
  101. 6
  102. 7
  103. 8
  104. 9
  105. 153
  106. 370
  107. 371
  108. 407
  109. 1634
  110. 8208
  111. 9474
  112.  

  
Login to rate this answer.

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

Related Open Questions

Ads

Connect

twitter fb Linkedin GPlus RSS

Ads

Interview Question

 Ask Interview Question?

 

Latest Questions

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.