Thread: simple 5 digit

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    41

    simple 5 digit

    I need to have a user type in 5 single digit numbers spaced, which works, but how can i fix this so if they type in more then 5 digits, or try to type in a 2 digit number. For example 12345 should look like 1 2 3 4 5.

    I want to fix this so that if they type in 1112131415 it will give a message.

    Code:
     float choice;
    
      printf("Enter a five-digit number:");
      scanf("%d", &num);
    
      if ((choice == 0) || (choice == 9))
        {
      num1 = ((num % 10) / 1);
      num2 = ((num % 100) / 10);
      num3 = ((num % 1000) / 100);
      num4 = ((num % 10000) / 1000);
      num5 = ((num % 100000) / 10000);
      printf("%d  %d  %d  %d  %d\n", num5, num4, num3, num2, num1);
        }
      else
        {
          printf("Enter five single digit numbers 0 thru 9\n");
        }
      return 0;
    }

  2. #2
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    If (num/100000 > 1) you have a 6 digit number. If (num/10000) < 1, you have less then 5 digits.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  3. #3
    The C eater *munch*
    Join Date
    Oct 2006
    Posts
    101
    Quote Originally Posted by Happy_Reaper
    If (num/100000 > 1) you have a 6 digit number. If (num/10000) < 1, you have less then 5 digits.
    shouldn't it be
    Code:
    if(num/100000 >= 1)
    with your original code, if I type a number between 100000 to 199999, it will be considered as 5 digits...

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    41
    Quote Originally Posted by yxunomei
    shouldn't it be
    Code:
    if(num/100000 >= 1)
    with your original code, if I type a number between 100000 to 199999, it will be considered as 5 digits...
    Thanks
    Last edited by wonderpoop; 10-15-2006 at 08:30 PM.

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Code:
    #include <stdio.h>
    
    int main()
    {
      unsigned test_inputs[] = { 12345, 12, 1234567, 123, 1 };
      int i = 0, num_inputs = 5;
      char buffer[BUFSIZ];
    
      for(i = 0; i < num_inputs; i++)
      {
        sprintf(buffer, "%u", test_inputs[i]);
        printf("%u %s a 5 digit number\n", test_inputs[i], strlen(buffer) == 5 ? "is" : "is not");
      }
      return 0;
    }

  6. #6
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Quote Originally Posted by yxunomei
    shouldn't it be
    Code:
    if(num/100000 >= 1)
    with your original code, if I type a number between 100000 to 199999, it will be considered as 5 digits...
    Right...I guess we're even.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    41

    i made it better but....

    I did it this way, something we are learning as well, this works, but again, now I think I need to change my if statement. It works, but it is still buggy for example:

    [academ] $ digit5.out
    Enter a five-digit number:12345
    1 2 3 4 5

    [academ] $ digit5.out
    Enter a five-digit number:1112131415
    Enter four single digit number

    [academ] $ digit5.out
    Enter a five-digit number:123456
    2 3 4 5 6




    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main()
    {
      int num, num1, num2, num3, num4, num5;
      int rm1, rm2, rm3, rm4, rm5;
     
      printf("Enter a five-digit number:");
      scanf("%d", &num);
    
      if (num/10000 <= '1')
        {
      num1 = ((num % 10) / 1);
      rm1 = ((num1++) % 10);
      num2 = ((num % 100) / 10);
      rm2 = ((num2++) % 10);
      num3 = ((num % 1000) / 100);
      rm3 = ((num3++) % 10);
      num4 = ((num % 10000) / 1000);
      rm4 = ((num4++) % 10);
      num5 = ((num % 100000) / 10000);
      rm5 = ((num5++) % 10);
      printf("%d  %d  %d  %d  %d\n", rm5, rm4, rm3, rm2, rm1);
        }
      else
        {
      printf("Enter five single digit numbers 0 thru 9\n");
        }
      return 0;
    }
    Last edited by Dave_Sinkula; 10-15-2006 at 09:42 PM. Reason: Moved (seemingly a continuation).

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    41

    Changes

    I now did it this way, but the if isn't completely working. If i put in 12345 I get 1 2 3 4 5, if i put in 1112131415 I get the else statement, but if i put in 123456 I get 2 3 4 5 6.

    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main()
    {
      int num, num1, num2, num3, num4, num5;
      int rm1, rm2, rm3, rm4, rm5;
     
      printf("Enter a five-digit number:");
      scanf("%d", &num);
    
      if (num/10000 <= '1')
        {
      num1 = ((num % 10) / 1);
      rm1 = ((num1++) % 10);
      num2 = ((num % 100) / 10);
      rm2 = ((num2++) % 10);
      num3 = ((num % 1000) / 100);
      rm3 = ((num3++) % 10);
      num4 = ((num % 10000) / 1000);
      rm4 = ((num4++) % 10);
      num5 = ((num % 100000) / 10000);
      rm5 = ((num5++) % 10);
      printf("%d  %d  %d  %d  %d\n", rm5, rm4, rm3, rm2, rm1);
        }
      else
        {
      printf("Enter five single digit numbers 0 thru 9\n");
        }
      return 0;
    }
    Last edited by Dave_Sinkula; 10-15-2006 at 09:55 PM. Reason: Quit starting new thread on the same topic.

  9. #9
    Registered User
    Join Date
    Dec 2005
    Location
    Australia - Melbourne
    Posts
    63
    check if the number is between 9999
    and 100000 exclusive

    Code:
    if(input > 9999 && input < 100000) {
     /* your code continues */
    a better way is to store the input as a string instead of an int. that way overfloat can be detected. basically all u have to do is check that all the contents of the array are digits and that the string is of length 5. then index through the array to print out the digits

    see code

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void) {
        char nums[7];
        int i;
        int flag=0;
        
        printf("Enter five digit number: ");
        scanf("%s", nums);
        
        /* check contents of  array */
        if(strlen(nums)==5) flag=1;
        for(i=0; i<5 && flag; i++) {
            if(nums[i] <='0' || nums[i] >= '9') flag =0;
        }
        
        /* print response */
        if(flag) {
            for(i=0; i<5; i++) printf("%c ", nums[i]);
        } else {
            printf("Input did not contain five digits\n");
        }
        system("PAUSE");
        return 0;
    }

  10. #10
    The C eater *munch*
    Join Date
    Oct 2006
    Posts
    101
    Quote Originally Posted by Happy_Reaper
    Right...I guess we're even.

    LOL ... ... your funny one

  11. #11
    The C eater *munch*
    Join Date
    Oct 2006
    Posts
    101
    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main()
    {
      int num, num1, num2, num3, num4, num5;
      int rm1, rm2, rm3, rm4, rm5;
     
      printf("Enter a five-digit number:");
      scanf("%d", &num);
    
      if (num/10000 <= '1') //  wrong!  see http://www.lookuptables.com/
                            //  you are doing if(num/10000 <= 49) instead of 1 
        {
      num1 = ((num % 10) / 1);
      rm1 = ((num1++) % 10);
      num2 = ((num % 100) / 10);
      rm2 = ((num2++) % 10);
      num3 = ((num % 1000) / 100);
      rm3 = ((num3++) % 10);
      num4 = ((num % 10000) / 1000);
      rm4 = ((num4++) % 10);
      num5 = ((num % 100000) / 10000);
      rm5 = ((num5++) % 10);
      printf("%d  %d  %d  %d  %d\n", rm5, rm4, rm3, rm2, rm1);
        }
      else
        {
      printf("Enter five single digit numbers 0 thru 9\n");
        }
      return 0;
    }
    Last edited by yxunomei; 10-16-2006 at 03:28 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Adding a Large number digit by digit
    By mejv3 in forum C Programming
    Replies: 23
    Last Post: 09-21-2007, 03:00 PM
  2. Adding a Large number digit by digit
    By mejv3 in forum C Programming
    Replies: 1
    Last Post: 09-14-2007, 03:28 AM
  3. Help With Stacks
    By penance in forum C Programming
    Replies: 7
    Last Post: 10-09-2005, 02:47 PM
  4. newbie programmer - needs help bad.
    By hortonheat in forum C Programming
    Replies: 17
    Last Post: 10-20-2004, 05:31 PM
  5. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM