Thread: Help me: Display 100's 10's 1's from an integer!

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    2

    Post Help me: Display 100's 10's 1's from an integer!

    Hi, I have a rather simple question.

    How can I obtain this result?

    ---------------------------------

    Enter an integer: 283

    number of hundreds: 2
    number of tens : 8
    number of ones : 3

    ----------------------------------

    thank you for your time

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    This isn't the best solution, but I'm lazy
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main ( void )
    {
      int i = 283, j = 2, array[3];
      while ( i != 0 ) {
        array[j--] = i % 10;
        i /= 10;
      }
      printf ( "%d %d %d\n", array[0], array[1], array[2] );
      return EXIT_SUCCESS;
    }
    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    2
    Thanks for trying but I'ts part of my class project, and we didn't get into arrays yet, so I can't use them.

    There must be a simpler way to do this.

    Anyone else know how?

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main ( void )
    {
      int i = 283;
      printf ( "Number of Ones: %d\n", i % 10 ), i /= 10;
      printf ( "Number of Tens: %d\n", i % 10 ), i /= 10;
      printf ( "Number of Hundreds: %d\n", i % 10 ), i /= 10;
      return EXIT_SUCCESS;
    }
    -Prelude
    My best code is written with the delete key.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Bah. Prelude, you did their homework for them! For future reference, you (the original poster) should actually read the FAQ, and possibly read the nice little sticky note at the top of the forum. You know the one, the one that said "MAKE AN EFFORT. POST YOUR CODE!" ... Oh forget it. It's a lost cause.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    I have no excuse, I forgot the most elegant way to solve this problem and couldn't stop myself

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. checking if a char * is an integer
    By gregulator in forum C Programming
    Replies: 5
    Last Post: 04-16-2004, 09:12 AM
  2. Conversion of character string to integer
    By supaben34 in forum C++ Programming
    Replies: 3
    Last Post: 10-30-2003, 04:34 AM
  3. Help with homework please
    By vleonte in forum C Programming
    Replies: 20
    Last Post: 10-13-2003, 11:16 AM
  4. Linked Lists Integer addition ? HELP Please??
    By green_eel in forum C Programming
    Replies: 3
    Last Post: 03-12-2003, 04:36 PM
  5. How to display a 64-bit integer?
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 02-13-2002, 11:13 PM