Thread: Problem I Can't solve...

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    7

    Problem I Can't solve...

    I'm just now learning to program using C at school. I've been working on a problem from my book for 3 days now and cannot find out how to make it work. If anyone can please help me out with this or post the code for it I would be forever grateful.

    The problem:
    Write a program that prompts a user for an integer value in the range 0 to 32,767 and then prints the individual digits of the numbers on a line with three spaces between the digits. The first line is to start with the leftmost digit and print all five digits; the second line is to start with the second digit from the left and print four digits, and so forth. For example, if the user enters 1234, then your program should print

    0 1 2 3 4
    1 2 3 4
    2 3 4
    3 4
    4


    ______________

    after working on it for some time I have only made it to here, which (if you try to run it) obviously doesn't work correctly.

    Code:
    // Import input/output library
    #include <stdio.h>
    
    // Main block
    int main (void)
    {
       // Declare int to store character in.
       int num[5];
    
       // Print a prompt
       printf("Please enter a number between or equal to 0 and 32767: ");
    
       // Grab user input
       scanf("%d",&num);
    
         // Check if value outside of range
         if ((num < 0) || (num > 32767))
             {
               printf("Outside of Range\n");
             }
         // Value is inside of range.
         else 
             {
               printf("%d   %d   %d   %d   %d\n", num[4], num[3], num[2], num[1], num[0]);
             }
       system("pause");
       return 0;
    }

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Well, you really use wrongly the array in C.
    num[5] is an array in which you can store 5 integers. Not 5 digits of an integer...
    So you ll have to separate each digit from the integer.
    E.g. to get the last digit you can do
    Code:
    int number;
    scanf("%d", &number);
    num[0] = num % 10;
    in this way you get what remains of the division of the integer entered with 10. Thus, the last digit. You store that at num[0]. The digit entered is supposed to be stored at "number".
    Figure out how to get all the digits.

    Also, what if somebody enters a 3 digit number? You have to figure out a way to handle this. Well, that's not hard once you find out how to separate the digit, since you can also count them with the same code.

    So find a way to separate the digits of an integer and we will work from there

  3. #3
    Registered User
    Join Date
    Jul 2008
    Posts
    7
    I actually just figured it out. My book had the % 10 thing, but I still have no idea how to check the rest of the digits. I only know that % 10 is the far right digit, so if you could elaborate on that a bit it would be great.

    Here's what I did, long and repetetive, but it works
    Code:
    // Import input/output library
    #include <stdio.h>
    
    // Main block
    int main (void)
    {
       // Declare ints
       int num, num0, num1, num2, num3, num4;
       int digit4, digit3, digit2, digit1, digit0;
    
       // Print a prompt
       printf("Please enter a number between or equal to 0 and 32767: ");
    
       // Get user input
       scanf("%d",&num);
    
         // Check if value outside of range
         if ((num < 0) || (num > 32767))
             {
               printf("Outside of Range\n");
             }
         // Value is inside of range
         else 
             {
             
                  // line 1 below
                  
                  num4 = num / 10000;
                  digit4 = num4 % 10;
               printf("%d   ", digit4);
    
                  num3 = num / 1000;
                  digit3 = num3 % 10;
               printf("%d   ", digit3);
               
                  num2 = num / 100;
                  digit2 = num2 % 10;
               printf("%d   ", digit2);
    
                  num1 = num / 10;
                  digit1 = num1 % 10;
               printf("%d   ", digit1);
               
                  num0 = num / 1;
                  digit0 = num0 % 10;
               printf("%d   ", digit0);
               
               // line 2 below
    
                  num3 = num / 1000;
                  digit3 = num3 % 10;
               printf("\n%d   ", digit3);
               
                  num2 = num / 100;
                  digit2 = num2 % 10;
               printf("%d   ", digit2);
    
                  num1 = num / 10;
                  digit1 = num1 % 10;
               printf("%d   ", digit1);
               
                  num0 = num / 1;
                  digit0 = num0 % 10;
               printf("%d   ", digit0);
               
               // line 3 below
               
               num2 = num / 100;
                  digit2 = num2 % 10;
               printf("\n%d   ", digit2);
    
                  num1 = num / 10;
                  digit1 = num1 % 10;
               printf("%d   ", digit1);
               
                  num0 = num / 1;
                  digit0 = num0 % 10;
               printf("%d   ", digit0);
               
               // line 4 below
               
               num1 = num / 10;
                  digit1 = num1 % 10;
               printf("\n%d   ", digit1);
               
                  num0 = num / 1;
                  digit0 = num0 % 10;
               printf("%d   ", digit0);
               
               // line 5 below
                                            
               num0 = num / 1;
                  digit0 = num0 % 10;
               printf("\n%d   \n", digit0);
               
               }
               
       system("pause");
       return 0;
    }

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    What &#37; does is get the remainder of a division.
    So 3 % 2 == 1, for example.
    The repetitive code can be generalized into a loop, if you know how to do that.
    The rest is just basic math (not much to do with C).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Solve This Problem Within 3 Hr Urgent Need
    By annum in forum C Programming
    Replies: 12
    Last Post: 10-04-2009, 09:56 AM
  2. problem solve
    By coolnarugodas in forum C Programming
    Replies: 7
    Last Post: 04-26-2005, 12:31 PM
  3. Replies: 2
    Last Post: 04-25-2005, 11:59 AM
  4. Bubble Sort / selection problem
    By Drew in forum C++ Programming
    Replies: 7
    Last Post: 08-26-2003, 03:23 PM
  5. problem cant solve please help.
    By sarah in forum C Programming
    Replies: 6
    Last Post: 09-03-2001, 01:32 PM