Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/articles/c-tutorials/)
-   -   Armstrong number check (http://www.go4expert.com/articles/armstrong-number-check-t2458/)

adroit89 7Jan2007 09:56

Armstrong number check
 
To check whether entered number is armstrong number or not.
Code: C

#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{
    int n,sum=0,rem=0,cube=0,n1,i;
    clrscr();
    printf("enter a number");
    scanf("%d",&n);
    n1=n;
    while(n!=0)
    {
        rem=n%10;
        cube=pow(rem,3);
        sum=sum+cube;
        n=n/10;
    }
    if(sum==n1)
        printf("angstrom");
    else
        printf("not angstrom");
    getch();
}


shabbir 7Jan2007 12:49

Re: Armstrong number check
 
You are printing the wrong output.
Code:

    if(sum==n1)
        printf("angstrom");
    else
        printf("not angstrom");

It should be armstrong number and not angstrom. For those who dont know what it is.

Armstrong number: An n-digit number equal to the sum of the nth powers of its digits.
There are no two-digit Armstrong numbers; and there are four three-digit Armstrong numbers: 153, 370, 371, and 407.
1x1x1 + 5x5x5 + 3x3x3 = 153

Peter_APIIT 12Apr2007 10:35

Re: Armstrong number check
 
I have read the above program and try to understand it but i cannot understand all. I only can calculate the 53 without the 1 and 370 without the 3. I don't know why. Your explanations is greatly appreciated by me and others.

n=n/10; -- Is it the 153 calculate 3 first, then 5 and finally 1.


Sorry for my stupidness.

Your help is greatly appreciated by me an others.

Peter_APIIT 12Apr2007 10:36

Re: Armstrong number check
 
Thanks for your help.


All times are GMT +5.5. The time now is 00:07.