Make A C Program??

Thread Starter

dogar sahab

Joined Mar 15, 2008
116
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???
 

Papabravo

Joined Feb 24, 2006
21,094
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?
 

Mark44

Joined Nov 26, 2007
628
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???
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:
Rich (BB code):
scanf("%s", num_buffer);
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
 

Papabravo

Joined Feb 24, 2006
21,094
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:
Rich (BB code):
scanf("%s", num_buffer);
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
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.
 

Mark44

Joined Nov 26, 2007
628
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
 

Thread Starter

dogar sahab

Joined Mar 15, 2008
116
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.
 

Mark44

Joined Nov 26, 2007
628
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.
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
 
Top