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

Showing Answers 1 - 8 of 8 Answers

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.

  Was this answer useful?  Yes

saurabh

  • Sep 16th, 2006
 

could you please send armstrong algo for the all digits

thnking you

saurabh

  Was this answer useful?  Yes

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

  Was this answer useful?  Yes

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;

}

}

  Was this answer useful?  Yes

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

  Was this answer useful?  Yes

Kapil S Shinde

  • Feb 25th, 2014
 

Code
  1. start

  2. Accept num

  3.  

  4. noOfDigit=calculate number of digits   //use counting digit function//use power function from math.h header file




  Was this answer useful?  Yes

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.  * Process the command line arguments; must provide

  19.  * a valid integer or range of integers.

  20.  *//**

  21.  * Main program; loop through the specified range

  22.  * of values and test each for narcissism; if

  23.  * the number is narcissistic, write it to

  24.  * standard output.

  25.  */"USAGE: %s start [end]n""%ld

  26. "

  Was this answer useful?  Yes

Give your answer:

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

 

Related Answered Questions

 

Related Open Questions