Thread: armstrong nos.

  1. #1
    Registered User joybanerjee39's Avatar
    Join Date
    Oct 2011
    Location
    kolkata
    Posts
    106

    armstrong nos.

    i am trying to write a program which displays the armstrong nos. between 1 and 999.
    an armstrong no. is a no. which is equal to the sum of the cubes of its constituent digits. eg. 153=1^3+5^3+3^3
    Code:
    #include <stdio.h>
    #include <math.h>
    #define  MAX_LIMIT 999
    #define  CUBE 3
    
    int main(void)
     {  int units=0,tens=0,no=0,hund=0;
        
        for(no=1;no<=MAX_LIMIT;no++)
        {  
       
          hund=no/100;        //TO FIND THE HUNDREDS DIGIT
          tens=(no%100)/10;    //TO FIND THE TENS DIGIT
          units=(no%100)%10;   //TO FIND THE UNITS DIGIT
          if(no==(pow(hund,CUBE))+(pow(tens,CUBE))+(pow(units,CUBE)))  // ARMSTRONG CONDITION
        
        printf("\n%d is a armstrong no.\n ",no);
      
        }
    
    
    
     
        return 0;
    
     }
    i want your reviews . are there simpler ways to solve this ?
    i have another question.
    before this concept i tried to get the digits of a no. using another concept.
    a no. can be expressed as the sum of the product of the digits and their place values , like 135=(1*100)+(3*10)+(5*1)
    so, i declared
    Code:
     int u,t,h,no;
    (h*100)+(t*10)+(u*1)=no;
    but, this gives an error lvalue required . why is it so?
    please help!!

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    About the code that had the error:

    The equal sign in C doesn't mean equal, it means "is assigned to", and always works from right to left. Your equal sign is on the wrong end of your assignment. Look at the line of code, from right to left, and you'll see the problem.
    Last edited by Adak; 11-16-2011 at 03:55 AM.

  3. #3
    Registered User joybanerjee39's Avatar
    Join Date
    Oct 2011
    Location
    kolkata
    Posts
    106
    Quote Originally Posted by Adak View Post
    About the code that had the error:

    The equal sign in C doesn't mean equal, it means "is assigned to", and always works from right to left. Your equal sign is on the wrong end of your assignment. Look at the line of code, from right to left, and you'll see the problem.
    thnx for ur reply
    but, if i want to implement the logic 135=(1*100)+(3*10)+(5*100)
    is there any way that u can suggest?

  4. #4
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by joybanerjee39 View Post
    thnx for ur reply
    but, if i want to implement the logic 135=(1*100)+(3*10)+(5*100)
    is there any way that u can suggest?
    Err... the way you want to try to separate the digits isn't possible .
    The only other way (other than using a loop) would be to put the number into a small array... and get the place values by indices .

  5. #5
    Registered User joybanerjee39's Avatar
    Join Date
    Oct 2011
    Location
    kolkata
    Posts
    106
    Quote Originally Posted by manasij7479 View Post
    Err... the way you want to try to separate the digits isn't possible .
    The only other way (other than using a loop) would be to put the number into a small array... and get the place values by indices .
    i have used ur advice and wrote a program. i have used a 2d array here.but, the problem is that if i give the no. as a whole (ie. not in form of 1st digit, 2nd digit etc.) the no. is stored as a whole (ie. the digits are not accessible separately) but, i want that the user will enter the no. as a whole and the digits can be accessed separately the code that i have written
    Code:
    #include<stdio.h>
    int main(void)
    {  int x[3][3],i=0;
       printf("enter  no. ");
       scanf("%d",&x[i]);
       printf("\n%d",x[0][0]);
       return 0;
    }
    now if i enter a no. say 135 then x[0][0] will show 135 not 1
    please help!

  6. #6
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    >i have used ur advice and wrote a program. i have used a 2d array here
    Why ?.... just get the integer input and use itoa (or the standard compliant alternatives presented here under the heading Portability )

  7. #7
    Registered User joybanerjee39's Avatar
    Join Date
    Oct 2011
    Location
    kolkata
    Posts
    106
    Quote Originally Posted by manasij7479 View Post
    >i have used ur advice and wrote a program. i have used a 2d array here
    Why ?.... just get the integer input and use itoa (or the standard compliant alternatives presented here under the heading Portability )
    oh! i didn't know about this function. thnx a lot for ur reference
    Last edited by joybanerjee39; 11-16-2011 at 10:56 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. armstrong problem statement in c++??
    By amerjamil in forum C++ Programming
    Replies: 2
    Last Post: 01-04-2011, 11:02 AM
  2. Replies: 11
    Last Post: 06-23-2010, 01:36 AM
  3. I made it to new Armstrong code to print up to 10 digit.
    By hardip patel in forum C Programming
    Replies: 4
    Last Post: 06-02-2010, 02:56 PM
  4. having trouble with armstrong program code
    By cooldude in forum C Programming
    Replies: 2
    Last Post: 09-06-2009, 11:58 PM
  5. Printing Armstrong Numbers from 1-500
    By duffmckagan in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 09:26 AM