Integer analysis, question

This is a discussion on Integer analysis, question within the C Programming forums, part of the General Programming Boards category; Design and code a program that will take in an integer number from the user. This number has to be ...

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    4

    Smile Integer analysis, question

    Design and code a program that will take in an integer number from the user. This number has to be between 1 and 9999 (inclusive). This number will then be analysed by your code as to how many 1000s, 100s, 10s, and 1s make up this number. For example, an entry of 4208 by the user should make your program output something like: 4208 is made up of 4 lots of 1000, 2 lots of 100, 0 lots of 10s, and 8 lots of 1s.
    So 4208 = 4 x 1000 + 2 x 100 + 0 x 10 + 8 x 1

    Another example is 233. The output should be something like 0 lots of 1000, 2 lots of 100, 3 lots of 10s, and 3 lots of 1s.

    Can i please have help with writing this code, and any examples please.

    This is how we have started:
    Code:
    #include <stdio.h>
    
    main()
    {
          int user_in;
          
          
          printf("enter an integer from value 1 to 9999:\n\n");
          scanf("%d", &user_in);
          
          
          if (user_in < 1000 > 0);
            printf("0 x 1000\n"); 
          if (user_in < 2000 > 999);
            printf("1 x 1000\n");
          if (user_in < 3000 > 1999);
            printf("2 x 1000\n");
          if (user_in < 4000 > 2999);
            printf("3 x 1000\n");
          if (user_in < 5000 > 3999);
            printf("4 x 1000\n");
          if (user_in < 6000 > 4999);
            printf("5 x 1000\n");
          
          
          
          
          
          
          
          system("pause");
          }
    thanks

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You need to look at each digit. There are many ways to do this, one of the easiest is to just keep dividing by 10 to start chopping it up. You also probably want to use the modulus operator % to help out.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    4
    Thanks buddy,

    Can you show me the fastest and simplest way you would do it please.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Probably the best way is to sit down with a pencil and paper and work out how you would dissect a number... then look in the C documentation to see if there are math functions that will help you achieve that goal...

    Quzah already mentioned modulous math (%) ... what is the result of 103 mod 10 ... 3, right? That should give you a pretty strong hint.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Two suggestions for you.

    1) If you want to use "stair step" type logic, it's usually far better to start with the greatest value, and step down in size, from there, to the smallest value.

    2) If you have a number, say 123, you can easily "peel" off the digits one at a time, by using this:

    Code:
    number=123
    count = 0
    while(number > 0) 
       count++;
       digit = number mod 10
       number /= 10;
    end of while loop
    When loop is 1, your digit will be 1's digit. When it's 2, your digit is the 10's digit, etc.

    Makes things a lot easier, imo.

  6. #6
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Code:
          if (user_in < 6000 > 4999);
    This!
    a < b > c is not C. you've to use && operator a < b && b > c.
    and if( A ) ; is not right.
    It's the same as
    Code:
    if(A) {  // do nothing  }

  7. #7
    N.L
    N.L is offline
    Registered User
    Join Date
    May 2011
    Posts
    2
    I am also attacking this question, I see that the modulus (%) can be used but could there please be a post to start the code of as i have tried for hours and have had no success! thanks

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Start your own thread. Post your own code. Read the homework policy, and use code tags. Or you could just actually search the forum since this problem comes up every week.


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,138
    Post your best try and we will help with that.

    An alternative way to using % would be a brute force try:

    subtract 1000 until the number is smaller than 1000. Remember the number of times you subtracted.
    subtract 100 until the number is smaller than 100. Remember the number of times you subtracted.
    subtract 10 until the number is smaller than 10. Remember the number of times you subtracted.
    subtract 1 until the number is smaller than 1. Remember the number of times you subtracted.

    Hint: there is a pattern in there
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  10. #10
    Registered User
    Join Date
    May 2011
    Posts
    4
    We are still having a few issues, do you think this is the best way to do this problem?

    Thanks guys

    Code:
    #include <stdio.h>
    
    main()
    {
    int user_in;
    
    
    printf("************* enter an integer from value 1 to 9999: ******************\n\n");
    scanf("%d", &user_in);
    
    
    if(user_in<1000){
    printf("0 x 1000\n"); 
    } 
    else
    if((user_in < 2000)&&(user_in >999)){
    printf("1 x 1000\n"); 
    } 
    else
    if((user_in < 3000)&&(user_in >1999)){
    printf("2 x 1000\n"); 
    }
    else
    if((user_in < 4000)&&(user_in >2999)){
    printf("3 x 1000\n"); 
    }
    else
    if((user_in < 5000)&&(user_in >3999)){
    printf("4 x 1000\n"); 
    }
    else
    if((user_in < 6000)&&(user_in >4999)){
    printf("5 x 1000\n"); 
    } 
    else
    if((user_in < 7000)&&(user_in >5999)){
    printf("6 x 1000\n"); 
    } 
    else
    if((user_in < 8000)&&(user_in >6999)){
    printf("7 x 1000\n"); 
    }
    else
    if((user_in < 9000)&&(user_in >7999)){
    printf("8 x 1000\n"); 
    }
    else
    if((user_in < 10000)&&(user_in >8999)){
    printf("9 x 1000\n"); 
    }
    
    
    else
    printf("**************** re-enter number *****************");
    
    system("pause");
    }

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Try it like this...
    Code:
    #include <stdio.h>
    
    int main (void)
      { int digit;
         int multiplier = 1;      
         unsigned int number;     // lets us go up to 9 digits
    
    
         // get number from the operator
    
        while (number > 0)
          { digit = number % 10
             printf("%d x %d\n",digit, multiplier);
             number /= 10;
             multiplier *= 10; }
    
       return 0; }

  12. #12
    Registered User
    Join Date
    May 2011
    Posts
    4
    hi, thanks for the help this is so far what we have written but there must be a way to short cut this otherwise we would be writting a 1500 line code by the time we do this for the 1s units.
    Code:
    #include <stdio.h>
    
    main()
    {
          int user_in;
          
          
          printf("*************  enter an integer from value 1 to 9999: ******************\n\n");
          scanf("%d", &user_in);
          
          
          if(user_in<1000){
           printf("0 x 1000\n"); 
          } 
          else
           if((user_in < 2000)&&(user_in >999)){
          printf("1 x 1000\n"); 
          }            
          else
           if((user_in < 3000)&&(user_in >1999)){
          printf("2 x 1000\n"); 
          }
          else
           if((user_in < 4000)&&(user_in >2999)){
          printf("3 x 1000\n"); 
          }
          else
           if((user_in < 5000)&&(user_in >3999)){
          printf("4 x 1000\n"); 
          }
          else
           if((user_in < 6000)&&(user_in >4999)){
          printf("5 x 1000\n"); 
          }      
          else
           if((user_in < 7000)&&(user_in >5999)){
          printf("6 x 1000\n"); 
          }      
          else
           if((user_in < 8000)&&(user_in >6999)){
          printf("7 x 1000\n"); 
          }
          else
           if((user_in < 9000)&&(user_in >7999)){
          printf("8 x 1000\n"); 
          }
          else
           if((user_in < 10000)&&(user_in >8999)){
          printf("9 x 1000\n"); 
          }
          
          
          else{
              printf("****************  re-enter number  *****************");
              }
          system("pause");
          }

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You aren't even reading this thread are you?


    Quzah.
    Hope is the first step on the road to disappointment.

  14. #14
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by larrysmiz View Post
    hi, thanks for the help this is so far what we have written but there must be a way to short cut this otherwise we would be writting a 1500 line code by the time we do this for the 1s units.
    HELLO...

    Go take a look at message #11 ... I showed you how to do up to 9 digits in 5 lines of code...
    Moreover; I could change 2 words in that and let you do up to 19 digits.
    Last edited by CommonTater; 05-11-2011 at 08:28 PM.

  15. #15
    N.L
    N.L is offline
    Registered User
    Join Date
    May 2011
    Posts
    2
    hi,
    thanks for the help we have now after some time figured out a suitable way to answer this, only that we now want the program to run continuously until stopped by the user, any suggestions would be aprreciated.
    Code:
     #include <stdio.h>
    
    main()
    {
          int user_in;
          
          printf("*************  enter an integer from value 1 to 9999: ******************\n\n");
          scanf("%d", &user_in);
          
          int a  = user_in/1000;
          int b = user_in % 1000 / 100;   
          int c = user_in % 100 / 10;    
          int d = user_in % 10 / 1;    
          
          
          printf("%d lots of 1000s\n\n",a);
          
          printf("%d lots of 100s\n\n",b);
          
          printf("%d lots of 10s\n\n",c);
          
          printf("%d lots of 1s\n\n",d); 
     
     
         
     
          
          if (user_in > 9999) {
                     printf("error");
                     }
          
          
          system("pause");
          }

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ integer array question .. plz help
    By amerjamil in forum C++ Programming
    Replies: 6
    Last Post: 12-21-2010, 12:43 AM
  2. Beam or Frame Analysis for Structural Analysis
    By greenmania in forum C Programming
    Replies: 3
    Last Post: 05-05-2010, 05:40 PM
  3. integer null question
    By transgalactic2 in forum C Programming
    Replies: 6
    Last Post: 01-06-2009, 12:22 PM
  4. question in rounding integer in c
    By DrGreat in forum C Programming
    Replies: 5
    Last Post: 04-13-2008, 08:13 AM
  5. integer format question
    By fsu_altek in forum C++ Programming
    Replies: 4
    Last Post: 10-11-2005, 05:31 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21