Make A C Program??

Discussion in 'Programmer's Corner' started by dogar sahab, Apr 13, 2008.

  1. dogar sahab

    Thread Starter Active Member

    Mar 15, 2008
    116
    0
    Write a C program that take a floating point number from the user and print the right most digit of number as well as print the right most digit of integral part???
     
  2. dogar sahab

    Thread Starter Active Member

    Mar 15, 2008
    116
    0
    please i need urgent reply
     
  3. Papabravo

    Expert

    Feb 24, 2006
    10,163
    1,796
    There is too little information for me to construct a meaningful reply. Two posts 3 minutes apart, what do you think this is -- a chat room?
     
  4. Mark44

    Well-Known Member

    Nov 26, 2007
    626
    1
    If I understand, when the user types in, for example, 23.475, the program should print 5 and 3.

    I would write the program like this:
    Input the number as a character string, not as a float or double, like this:
    Code ( (Unknown Language)):
    1.  
    2. scanf("%s", num_buffer);
    3.  
    Here num_buffer would have already been declared as a character array of some suitable size.

    Now, since the array contains all the digit characters and the period for the decimal point, you can search the string for the last nonnull character in the string. That will be the rightmost digit in the fractional part of the number. You can also search the string for the character just to the left of the decimal point. That will be the units' digit.

    Mark
     
  5. Papabravo

    Expert

    Feb 24, 2006
    10,163
    1,796
    I wasn't sure we could assume that "input from the user" meant "character string input from a keyboard" It could have been binary data in IEEE-754 single precision which would have suggested one approach or it could have been double precision which would require an alternate approach. The OP did not specify a computing platfor or a language. I still think he needs to define his requirements.

    If as you have assumed it is string input then the task is trivial as you have suggested.
     
  6. Mark44

    Well-Known Member

    Nov 26, 2007
    626
    1
    Papabravo,
    I'm interpreting what the OP wrote (take a floating point number from the user) in the most literal sense; i.e., that the user types in a number. In this case the number comes from the user as a sequence of characters, and not in binary form as single or double precision binary data.

    If, on the other hand, he has to deal with a float or a double, it's not too hard to get at the digit to the left of the decimal point, but will require some work to get at the least-significant decimal place digit. Given the OP's problem statement, it seemed to me that the simplest solution would be to bypass the usual conversion of a string of characters into a float or double via scanf or cin, and just deal with the input string directly.
    Mark
     
  7. dogar sahab

    Thread Starter Active Member

    Mar 15, 2008
    116
    0
    i am literally talking about floating-point number...It has nothing to do with stings...
    e.g
    if the user enters the number 32.415
    then the output displayed on the monitor must be:
    The right most digit of the number is 5
    The right most digit of the integral part is 2.
     
  8. dogar sahab

    Thread Starter Active Member

    Mar 15, 2008
    116
    0
    *character strings
     
  9. Mark44

    Well-Known Member

    Nov 26, 2007
    626
    1
    It has everything to do with character strings.
    Using your example, the user types the characters '3', '2', '.', '4', '1', and '5'. Your program can choose to interpret these characters as a string of characters or as a floating point number, depending on how scanf() is used (or the cin stream, if you're doing it that way).

    Take a look at what I said earlier in this thread. I've provided a way for you to do what you're asking.

    If you actually need to use what the user typed as a number (which you said nothing about), the string can be converted into a float or a double with the use of atof() or atod(), respectively.

    Mark
     
  10. dogar sahab

    Thread Starter Active Member

    Mar 15, 2008
    116
    0
    okay..thanks for helping me out...I gonna try this on C compiler...thanks for your anticipation...
     
Loading...