need help with c programming hmwk

This is a discussion on need help with c programming hmwk within the C Programming forums, part of the General Programming Boards category; Can someone please help me with this assignment? I'm a programming noob and any help would be greatly appreciated. Write ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    24

    need help with c programming hmwk

    Can someone please help me with this assignment? I'm a programming noob and any help would be greatly appreciated. Write a program that receives an integer from user and prints out the number of thousands,
    hundreds, tens and ones in it. You need to specify the number of thousands, hundreds, tens and
    ones, even if the number is larger than 9,999. [Hint: if a and b are two integers, then a/b is the
    quotient obtained by dividing a by b and a%b is the remainder obtained by dividing a by b].

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Well, there's a pretty good hint about divide and modulo in the description, so you may want to look into that.

    Once you have written a bit of code, you can post it here, with any questions you have, but we don't do other peoples homework. Just like the discussion board for the English language may be able to help you form a sentence correctly, but they won't write your 500 word essay for you. [This program would be about 5-10 lines depending on how you do it, so it's much less than a 500 word essay.]

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    24
    This is my program thus far:
    {int a, b, thousands, hundreds, tens, ones;
    b=1000;
    printf("Enter a number: ");
    scanf("%f", &a);
    thousands= a/b;
    printf("%d", thousands);
    So I want to divide the integer by 1000 to get a number that is truncated leaving the correct amount of thousands. When I run this and enter an integer, i get garbage numbers. Is this the way I should go about this assignment? How can I employ the hint given to make this easier.

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Nobody in the board would say that is a code. The code with no main and include's and most of all with no return. Perhaps the code tag. Here we go i will do it for you

    Code:
    #include <stdio.h>
    int main()
    {
        int a, b, thousands, hundreds, tens, ones;
        b=1000;
    
        printf("Enter a number: ");
        scanf("&#37;f", &a);
        thousands= a/b;
        printf("%d", thousands);
    
        getchar();
        return 0;
    }
    Now, from here you start. Look at basics of C, understand what you need to do at initial. Rob had given some good links have a look at it

    ssharish2005

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by nafix View Post
    This is my program thus far:
    {int a, b, thousands, hundreds, tens, ones;
    b=1000;
    printf("Enter a number: ");
    scanf("%f", &a);
    thousands= a/b;
    printf("%d", thousands);
    So I want to divide the integer by 1000 to get a number that is truncated leaving the correct amount of thousands. When I run this and enter an integer, i get garbage numbers. Is this the way I should go about this assignment? How can I employ the hint given to make this easier.
    %f is a "floating point" format for scanf - it reads in values like 2.5 or 1.9272937. If you enable warnings (at least on gcc), it should give you a warning when you compine an int inptu to scanf with a float or char format specifier.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Sep 2007
    Posts
    24
    [QUOTE=nafix;669551]This is my program thus far:
    Code:
    #include <stdio.h>
    int main(void)
    {int a, b, thousands, hundreds, tens, ones;
    b=1000;
    printf("Enter a number(integer): ");
    scanf("%d", &a);
    thousands= a/b;
    printf("%d", thousands);
    
    return 0;
    }
    So far, this gives me 4 for the thousands which is what I want. But I'm having trouble getting the hundreds, tens, and ones. And I can't think of a way to obtain the other decimal places. Can someone give me a nudge in the right direction?

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    minus the thousands (thousands * 1000) and then do the same thing for 100.

  9. #9
    Registered User
    Join Date
    Sep 2007
    Posts
    24
    but the program has to work for integers over 9,999. subtracting out 1000 wouldn't work for things over that number, right?

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Of course, there is a big hint in the original problem defintion. Try reading that again.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Registered User
    Join Date
    Sep 2007
    Posts
    24
    Is there a way to print out the remainder of integer/ 1000? So if i divide 12225 by 1000 to get 12.225, can I print out the 2 for hundreds, 2 for tens, and 5 for ones? Or can I print out the remainder next divided by 100 for hundreds, then print remainder divided by 10 for tens place and so on? How would I print out just the remainder.
    Last edited by nafix; 09-07-2007 at 07:38 PM.

  12. #12
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    num&#37;100

    modulo

  13. #13
    Registered User
    Join Date
    Sep 2007
    Posts
    24
    do i put that in a print function? how would i go about initializing modulo in code?

  14. #14
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129

  15. #15
    Registered User
    Join Date
    Sep 2007
    Posts
    24
    [QUOTE=nafix;669560][QUOTE=nafix;669551]This is my program thus far:
    Code:
    #include <stdio.h>
    int main(void)
    {int a, b, c, thousands, hundreds, tens, ones;
    b=1000;
    printf("Enter a number(integer): ");
    scanf("&#37;d", &a);
    thousands= a/b;
    printf("%d", thousands);
    c= a%b
    hundreds= c/100;
    printf("%d Hundreds", hundreds);
    
    return 0;
    }
    This is my revised code, but now I'm printing out 0 for thousands and hundreds. WHat am I doing wrong?

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

Similar Threads

  1. NEED last minute hmwk help...$$
    By TOPFLOR in forum C Programming
    Replies: 4
    Last Post: 08-14-2006, 04:23 PM
  2. couple hmwk questions...
    By slimwboozha in forum C Programming
    Replies: 2
    Last Post: 05-06-2003, 08:56 PM

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