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.

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

Code

start

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

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 )

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

## Answer Question

Click here to Login / Register your free account

## Related Answered Questions

## Related Open Questions