Thread: Been working on this problem for awhile.

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    6

    Been working on this problem for awhile.

    I been trying to teach my self C and i been working on this problem for a while. Help please.


    Write a program that would take a integer number from the user and separate it into numbers
    with appropriate place values.
    Example: User inputs 4500 and your program needs to output the following:
    User Input Number is 4500.
    4500 Contains:
    4 Thousands
    5 Hundreds
    0 Tens
    0 Units

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by hotchico2007 View Post
    I been trying to teach my self C and i been working on this problem for a while. Help please.
    Okay, post the code you have tried already.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    6
    Code:
    #include <stdio.h>
    
    int main()
    {
    int T, H, t, U;
    printf(" Input a number=");
    scanf("%d,%d,%d,%d",&T,&H,&t,&U);
    printf(" displayed number is %d", T, H, t, U);
    
    return 0;
    }

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    %d does not stand for Digit, it stands for Decimal integer, ie, a base 10 whole number.

    4500 is just one %d. 450000001 is also just one %d. So you only need to read in one.

    Once you have that number, you MAY want to use division AND modulus (%) on it. Modulus will give you a remainder.

    x = 4844/1000;

    x will be 4, since C int arithmetic always rounds down.

    y = 4844%1000;

    x will be 844.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    You might consider using %c (eg: read a single character). Also, always check the return value of scanf.

    >> printf(" displayed number is %d", T, H, t, U);

    That's only going to print one of the numbers, obviously.

  6. #6
    Registered User
    Join Date
    Sep 2009
    Posts
    6

    Cool Im still confused

    Well i appreciate the advice but i dont seem to understand how to go about this problem. ITs quite frustrating. ANy more help yall can give me? I just dont know what i should do next.

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Well, before we can help you we need to know what exactly you're confused about.

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by hotchico2007 View Post
    Well i appreciate the advice but i dont seem to understand how to go about this problem. ITs quite frustrating. ANy more help yall can give me? I just dont know what i should do next.
    Here's a suggestion:

    1. Read in ONE number (eg, 4500)
    2. print out that number
    3. perform some arithmetic on the number -- anything. Multiply by 2.
    4. print the new number out


    Once you can do that, you are most of the way there.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    It's just using a trick of base 10 arithmetic.

    In the number 12, you have (from right to left), two one's, and 1 ten, or : 10 + 2.

    so mathematically, you can tell what each digit in a number is by:

    1) Dividing it by 10
    2) If the number has been divided evenly by 10, your digit in the one's column, was a 0:
    e.g.: 10, 80, 100, 120, 1080, etc., all divide by 10, with no remainder

    3) If there was a remainder, that is your digit:
    e.g.: 15/10 leaves 5, 101/10 leaves 1, 189/10 leaves 9, 1055/10 leaves 5 remainder

    You can keep dividing and subtracting the remainder from the original number, (that remainder being the digit you want to print), until your original number is zero. Then you're done.

    You can also use itoa() and change your number into a string. Then use something like this:

    Code:
    i = 0;
    while (string[i])
       printf("%c", string[i++]);
    Last edited by Adak; 09-25-2009 at 02:40 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem working PlaySound()...
    By yaya in forum C++ Programming
    Replies: 1
    Last Post: 12-30-2007, 03:17 AM
  2. Bin packing problem....
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 0
    Last Post: 08-01-2005, 05:20 AM
  3. small reference problem
    By DavidP in forum C++ Programming
    Replies: 6
    Last Post: 06-21-2004, 07:29 PM
  4. Replies: 5
    Last Post: 12-03-2003, 05:47 PM
  5. inStream practice problem not working?
    By correlcj in forum C++ Programming
    Replies: 2
    Last Post: 10-27-2002, 05:58 PM