473,785 Members | 2,488 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Digits after the decimal point

I am doing Engineering(B.T ech) in Computer Science.

I have a question for which i am struggling to write a C code(program).
It struck me when we were being taught about a program which counts the
number of digits in a given number.
I request to help me out in solving the below said question.

Ask the user to enter a decimal/float number(eg. 32.8952), then count
the number of digits in that number after the decimal point(4 in this
case).

eg: If user enters 45.99420 then we should get 5, which are the number
of digits after the decimal point.

- Kuljit

Feb 15 '06
18 12170
gamehack wrote:
Kuljit wrote:
I am doing Engineering(B.T ech) in Computer Science.

I have a question for which i am struggling to write a C code(program).
It struck me when we were being taught about a program which counts the
number of digits in a given number.
I request to help me out in solving the below said question.

Ask the user to enter a decimal/float number(eg. 32.8952), then count
the number of digits in that number after the decimal point(4 in this
case).

eg: If user enters 45.99420 then we should get 5, which are the number
of digits after the decimal point.

- Kuljit
Well...
I'll tell you the algorithm for doing it, you do the code :)
1. Allocate a buffer to hold the input as a string(eg char input[80])
2. Use scanf(hint: scanf("%s", input))


Bigger hint. Don't use scanf("%s", input) ever. It allows the user to
scribble all over your memory by typing in more characters than you have
allowed for.
3. Start walking the string character by character(assum es ASCII)
No need to assume ASCII. C provides perfectly good methods for
identifying digits, '+', '-' and '.'
4. When you encounter the char x = '.' start increment a counter
5. Finish incrementing when you've reached '\0'
You forgot to check for invalid input.
Hope you can write the code yourself.


Yes, the OP does have to attempt to write the code. Having done so
people here can assist with any C questions.
--
Flash Gordon
Living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidlines and intro -
http://clc-wiki.net/wiki/Intro_to_clc
Feb 15 '06 #11
gamehack wrote:
Kuljit wrote:

Ask the user to enter a decimal/float number(eg. 32.8952), then count
the number of digits in that number after the decimal point(4 in this
case).
I'll tell you the algorithm for doing it, you do the code :)
1. Allocate a buffer to hold the input as a string(eg char input[80])
2. Use scanf(hint: scanf("%s", input))


I must have missed the part where the OP wanted to cause
a buffer overflow. (Never use scanf with %s).
3. Start walking the string character by character(assum es ASCII)


You don't have to assume ASCII to walk a string character
by character.

Feb 15 '06 #12
On 15 Feb 2006 10:03:18 -0800, "gamehack" <ga******@gmail .com> wrote:

Kuljit wrote:
I am doing Engineering(B.T ech) in Computer Science.

I have a question for which i am struggling to write a C code(program).
It struck me when we were being taught about a program which counts the
number of digits in a given number.
I request to help me out in solving the below said question.

Ask the user to enter a decimal/float number(eg. 32.8952), then count
the number of digits in that number after the decimal point(4 in this
case).

eg: If user enters 45.99420 then we should get 5, which are the number
of digits after the decimal point.

- Kuljit
Well...
I'll tell you the algorithm for doing it, you do the code :)
1. Allocate a buffer to hold the input as a string(eg char input[80])
2. Use scanf(hint: scanf("%s", input))


fgets() is a much better choice than using scanf() with an
unrestricted %s.
3. Start walking the string character by character(assum es ASCII)
4. When you encounter the char x = '.' start increment a counter
5. Finish incrementing when you've reached '\0'

Hope you can write the code yourself.

Remove del for email
Feb 17 '06 #13
Hello everyone,
Thankyou for your help. i have written the code as you have said to
accept it as a string and got the output, but as many of you have said
this is not a floating point problem.

So can we ask the user to enter the decimal number in floating point
value and then determine from that what the user typed.

Hope you wil be able to solve this problem also.

- Kuljit

Feb 23 '06 #14
Kuljit wrote:
Hello everyone,
Thankyou for your help. i have written the code as you have said to
accept it as a string and got the output, but as many of you have said
this is not a floating point problem.
You should quote what you're replying to, even if you're replying to
yourself.
So can we ask the user to enter the decimal number in floating point
value and then determine from that what the user typed.

Hope you wil be able to solve this problem also.


Luckily for you, I'm using Google at the moment (grrrr) and can
actually see what the original problem was, so I may be able to help...

Are you now referring to numbers of the -1.23e-12 format? If yes:

The only difference is figuring out the rules that these numbers
follow, and checking the input against them (read in, and walk through
the string). The rules are (I may get this wrong, please do check):

<float> ::= <fixed><exponen t><integer>.
<fixed> ::= [<sign>]<unsigned>['.' [<unsigned>]].
<exponent> ::= 'e' | 'E' | 'd' | 'D'.
<integer> ::= [<sign>]<unsigned>.
<sign> ::= '+' | '-'.
<unsigned> ::= <digit>[<digit>]*.
<digit> ::= '0' ... '9'.

Now, using the grammar above, depending on where you encounter '\0',
you'll know exactly what sort of the number was entered (including
integers, both signed and unsigned).

Note:
<> token
::= is
| one of
[] optional
* zero or more
. end of the rule

--
BR, Vladimir

Feb 23 '06 #15

Vladimir S. Oka wrote:
Kuljit wrote:
Hello everyone,
Thankyou for your help. i have written the code as you have said to
accept it as a string and got the output, but as many of you have said
this is not a floating point problem.
You should quote what you're replying to, even if you're replying to
yourself.
So can we ask the user to enter the decimal number in floating point
value and then determine from that what the user typed.

Hope you wil be able to solve this problem also.


Luckily for you, I'm using Google at the moment (grrrr) and can
actually see what the original problem was, so I may be able to help...

Are you now referring to numbers of the -1.23e-12 format? If yes:

The only difference is figuring out the rules that these numbers
follow, and checking the input against them (read in, and walk through
the string). The rules are (I may get this wrong, please do check):

<float> ::= <fixed><exponen t><integer>.
<fixed> ::= [<sign>]<unsigned>['.' [<unsigned>]].
<exponent> ::= 'e' | 'E' | 'd' | 'D'.
<integer> ::= [<sign>]<unsigned>.
<sign> ::= '+' | '-'.
<unsigned> ::= <digit>[<digit>]*.
<digit> ::= '0' ... '9'.


Obviously:

<digit> ::= '0' | '1' | ... | '9'.

:-(

Now, using the grammar above, depending on where you encounter '\0',
you'll know exactly what sort of the number was entered (including
integers, both signed and unsigned).

Note:
<> token
::= is
| one of
[] optional
* zero or more
. end of the rule

--
BR, Vladimir


Feb 23 '06 #16
Dear Vladmir,
Thankyou for your reply.
I think you havent understood what i meant. My original text was

Kuljit wrote:
I have a question for which i am struggling to write a C code(program).
It struck me when we were being taught about a program which counts the
number of digits in a given number.
I request to help me out in solving the below said question. Ask the user to enter a decimal/float number(eg. 32.8952), then count
the number of digits in that number after the decimal point(4 in this
case).
eg: If user enters 45.99420 then we should get 5, which are the number
of digits after the decimal point.


so i want to count the number of digits after the decimal point as i
have said in my quoted text. But instead of accepting it in a string, i
want to accept it in float datatype.

So please try to help me out with this problem.

-Kuljit

Feb 24 '06 #17
On 2006-02-24, Kuljit <ku**********@g mail.com> wrote:
Dear Vladmir,
Thankyou for your reply.
I think you havent understood what i meant. My original text was

Kuljit wrote:
I have a question for which i am struggling to write a C code(program).
It struck me when we were being taught about a program which counts the
number of digits in a given number.
I request to help me out in solving the below said question.

Ask the user to enter a decimal/float number(eg. 32.8952), then count
the number of digits in that number after the decimal point(4 in this
case).


eg: If user enters 45.99420 then we should get 5, which are the number
of digits after the decimal point.


so i want to count the number of digits after the decimal point as i
have said in my quoted text. But instead of accepting it in a string, i
want to accept it in float datatype.

So please try to help me out with this problem.

-Kuljit


You can read it into a string first, count your decimal places
and then convert the string to a float : google will help you convert a
string to a float. All the things to be careful about in parsing the
string to detect number of decimal places are already covered in the thread.

I can see no reason why you would want to calculate decimal places
from the float variable when you can have the string representation to
start with. You can store the input in a float afterwards maybe?

See here : http://tinyurl.com/heyck

This covers using scanf, strtod and others with the usual style and
robustness arguments prevalent in all threads to do with string to
number conversions :)
--
Remove evomer to reply
Feb 24 '06 #18
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Kuljit wrote:
[snip]
Ask the user to enter a decimal/float number(eg. 32.8952), then count
the number of digits in that number after the decimal point(4 in this
case).


eg: If user enters 45.99420 then we should get 5, which are the number
of digits after the decimal point.


so i want to count the number of digits after the decimal point as i
have said in my quoted text. But instead of accepting it in a string, i
want to accept it in float datatype.


Unfortunately, if (as you say) you want to count the number of digits
after the decimal point in a /floating point number/ (and not a string),
then your examples are a bit out of whack.

Consider that the user enters the string "32.8952", and you convert it
to a floatingpoint number (your first example). There are clearly 4
significant digits following the decimal point. *But*, 32.8952 /as a
rational number/ is the same as 32.89520, which (by your second example)
you wish to count as 5 digits after the decimal point. /And/, the number
is the same as 32.895200, which is 6 decimal places (by extension of
your two examples).

Do you see the problem? Which interpretation of the number is correct
for your count?

Also, when working with /floatingpoint/ numbers, floatingpoint is
inaccurate: a number /entered/ as 45.99420 might actually become
45.994197827734 , which will have a different count of significant
decimal places than you expect

So, to satisfy your count requirement, you cannot count the digits in
the floatingpoint datatype.

- --

Lew Pitcher, IT Specialist, Corporate Technology Solutions,
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer's)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFD/y9/agVFX4UWr64RAi9 eAJ4uhYQiboRiLl x6FjtRvS9sb8zGt wCfa81k
BMvnCHhESl8LOYm Rfeg0YeQ=
=Brvz
-----END PGP SIGNATURE-----
Feb 24 '06 #19

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
10557
by: Peter Blatt | last post by:
Does 5 represent the total numer of digits (including the fractional portion) or only the number of places BEFORE the decimal point? Moreover does the number include the decimal point? Are there differences between the databases servers ? Peter
4
9733
by: italia | last post by:
I changed the Fieldsize Property from text to Long Integer and Decimal Places = 6. I had decimals in the original field. But after the transfer, the digits after the decimals are gone. Now even after I have change the Fieldsize propert to Decimal with Scale = 2, the digits after the decimal are not seen. For eg. If the text was 16.27. After I changed to long integer, it
10
4244
by: guidosh | last post by:
Hello, I'm trying to write a printf statement that sets the field width when printing a number. I'm using this: printf("%*", fieldwidth, num_to_print); However, I can't figure out how to get the number of digits in the number (which I would then assign to the 'fieldwidth' variable above).
17
2122
by: =?Utf-8?B?TWljaGVsIFBvc3NldGggW01DUF0=?= | last post by:
Hello , Does someone knows a simple way of how to get the nr of fraction digits ? example : 1.23 would give a result of 2 1.234 would give a result of 3 Yes
0
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10325
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10147
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10091
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8972
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7499
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6739
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4050
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2879
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.