Armstrong number check

Discussion in 'C' started by adroit89, Jan 7, 2007.

  1. adroit89

    adroit89 New Member

    Joined:
    Jan 5, 2007
    Messages:
    24
    Likes Received:
    0
    Trophy Points:
    0
    To check whether entered number is armstrong number or not.
    Code:
    #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();
    }
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    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
     
  3. Peter_APIIT

    Peter_APIIT New Member

    Joined:
    Apr 11, 2007
    Messages:
    92
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    Malaysia
    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.
     
  4. Peter_APIIT

    Peter_APIIT New Member

    Joined:
    Apr 11, 2007
    Messages:
    92
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    Malaysia
    Thanks for your help.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice