Thread: Help with simple program

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

    Help with simple program

    Hey all, there might be a solution to my problem in the tutorials but unfortunately I've procrastinated to the point where I need a solution a little quicker :/

    The program I am writing is designed to take a number from a user and determine how many of each digit it contains.. pretty basic.

    For example, 2345. Has 2 thousands, 3 hundreds, 4 tens and 5 ones.

    I have the program written and everything works fine except I always get '0' for ones. I was hoping someone could take a look at it and let me know what I'm overlooking.

    Code:
    #include <stdio.h>
    
    main(void)
    {
            int a, b, c, d, thousand, hundred, ten, one;
            thousand = 1000;
            hundred = 100;
            ten = 10;
            one = 1;
    
            printf("Enter a Number: \n");
            scanf("%d", &a);
    
            b = a%thousand;
            c = b%hundred;
            d = c%one;
    
            printf("%d THOUSANDS\n", a / thousand);
            printf("%d HUNDREDS\n", b / hundred);
            printf("%d TENS\n", c / ten);
            printf("%d ONES\n", d);
    
            return 0;
    
    
    }
    I am new to C so I know there is probably a quicker way to do all of this but you live you learn, eh?

    Thanks in advance to anyone who takes up any of their time to look it over.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    any number mod 1 gives 0
    try
    Code:
     d = c&#37;ten;
    Kurt

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    26
    Now that you pointed it out, that seems like an obvious mistake. *slaps forehead*

    Thank you very much for the help!

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    i think you have a class mate asking the same question in this board. It wasnt too long. I guess it was yesterday.

    ssharish2005

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    26
    Yeah, right after I asked the question I noticed that guy's thread. Sorry about posting a similar one.

    /me takes time to check first next time

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with a very simple program
    By htdefiant in forum C++ Programming
    Replies: 13
    Last Post: 08-14-2007, 01:27 PM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. [Help] Simple Array/Pointer Program
    By sandwater in forum C Programming
    Replies: 3
    Last Post: 03-30-2007, 02:42 PM
  4. simple silly program
    By verbity in forum C Programming
    Replies: 5
    Last Post: 12-19-2006, 06:06 PM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM