Thread: Finding the first digit in a number....help

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    57

    Question Finding the first digit in a number....help

    Hey guys, just wanted to first off say thanks for all the help in the past.

    I was wondering how to go about finding the first digit from a number inputted into an array.

    Thank you for any help.

    ex.
    enter the numbers to use:
    9
    45
    682
    120

    output:
    the numbers are 9 4 6 and 1


    Here is what I have, the array goes to a max of 15 for the moment, but I allow the user to input as large an array as they want up to that number. Then the user is to input it's numbers.
    Code:
    #include <stdio.h>
    
    #define MAX 15
    int main()
    {
    
    int array[MAX]; /*Array to place numbers in*/
    int num;        /*Amount of number to use in game*/
    int num1;       /*Number to be inputted for game*/
    int i, j, k;
    
    
      printf("How many numbers would you like to have?\n");
      scanf("%d", &num);
    
        for(i = 0; i < num ; i++){  /*Starts loop to input numbers into array*/
          scanf("%d", &num1);
          array[i] = num1;   
          }
           
      printf("\n");
      
        for(k = 0; k < num; k++){  /*Loop prints the array*/
          printf("%d ", array[k]);
          }
           
      return 0;
    }

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    57
    for the moment i have found it on this thread.
    Code:
     while (num1 >= 10)
        num1 /= 10;
    would there be a better to go about this? Such as turning it to a string?

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Such as turning it to a string?
    How do you think the code turns a number into a decimal string to begin with?

    Yes, a loop consisting of /10 and %10.
    And some extra baggage as well.

    Add a check for negative numbers, and you're golden.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    Such as turning it to a string?
    Yes, definitely... well, actually, forget about turning it into an integer. If all you're going to do is find the first digit, you might as well this:
    Code:
    char mynumber[20];
    
    /*in loop*/
    scanf("%s", &mynumber);
    array[i] = (int) mynumber[0] - (int) '0');
    Obviously this is incomplete but the idea is there; also you'll want to protect against the evils of the scanf("%s") security hole, and look for the first actual digit [skipping '-' if it's there] in the string. "I'll leave that to the reader as an exercise."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  2. Replies: 2
    Last Post: 02-08-2009, 09:26 PM
  3. Adding a Large number digit by digit
    By mejv3 in forum C Programming
    Replies: 1
    Last Post: 09-14-2007, 03:28 AM
  4. how to read a digit of a floating point number?????
    By spicy_centipede in forum C Programming
    Replies: 15
    Last Post: 07-14-2007, 11:43 AM
  5. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 05:00 PM