473,769 Members | 2,355 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 #1
18 12168

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))
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.

Feb 15 '06 #2
"Kuljit" writes:
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.


Read the input into a string. getline() would probably be a good choice.
Search for the decimal point and then count digits until you get to the end
of the number. Note that you can look at a character in a string, s, with
s[i], much like an array.
Feb 15 '06 #3
"osmium" writes:

<snip>

Never mind. Wrong news froup.
Feb 15 '06 #4

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))
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.


While doing this, make sure that what the user inputs is an actual
valid number, i.e.:
input like +a1b2.53.678.98 4 is not valid.

--
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it's not too late)
(... and that it still works...)

Feb 15 '06 #5

Nelu wrote:
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))
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.


While doing this, make sure that what the user inputs is an actual
valid number, i.e.:
input like +a1b2.53.678.98 4 is not valid.


Even better. You can use only getchar. No need to allocate memory for a
string.
You just have to know how to parse a number: [sign] [integer part] .
[fractional part]
What's between [ and ] is optional.

--
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it's not too late)
(... and that it still works...)

Feb 15 '06 #6
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

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.


I think that you are going to have to redefine the problem before you
can get your solution.

Using your example above, how many digits to the right of the decimal
point does
45.9942
and
45.994200
have?

If the count is /not/ 5 (and I suspect that it won't be), then you are
/not/ counting decimal places in a floating point number. You are,
instead, counting digit characters that follow a '.' in a string.

(FWIW, you'd have other problems with floatingpoint numbers; often the
numerical value of a floatingpoint number does not match it's
floatingpoint value, so 45.99420 might actually count 10 decimal places
because it is actually stored as 45.9941789301 or something.)

HTH
- --

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

iD8DBQFD825MagV FX4UWr64RAqypAJ 9Ly9NJPaw4IA6cy VJV+vuyk5eZxACe MQf0
qnivmuy3/BTPu8WsLeK0+9o=
=FNJR
-----END PGP SIGNATURE-----
Feb 15 '06 #7
Nelu schrieb:
Nelu wrote:
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))
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.


While doing this, make sure that what the user inputs is an actual
valid number, i.e.:
input like +a1b2.53.678.98 4 is not valid.


Even better. You can use only getchar. No need to allocate memory for a
string.
You just have to know how to parse a number: [sign] [integer part] .
[fractional part]
What's between [ and ] is optional.


Not entirely.
"." does not count...

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Feb 15 '06 #8

Michael Mair wrote:
Nelu schrieb:

Even better. You can use only getchar. No need to allocate memory for a
string.
You just have to know how to parse a number: [sign] [integer part] .
[fractional part]
What's between [ and ] is optional.


Not entirely.
"." does not count...

Right. '.' doesn't stand alone as a number. My mistake.

--
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it's not too late)
(... and that it still works...)

Feb 15 '06 #9
Lew Pitcher wrote:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

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.

I think that you are going to have to redefine the problem before you
can get your solution.

Using your example above, how many digits to the right of the decimal
point does
45.9942
and
45.994200
have?

If the count is /not/ 5 (and I suspect that it won't be), then you are
/not/ counting decimal places in a floating point number. You are,
instead, counting digit characters that follow a '.' in a string.

(FWIW, you'd have other problems with floatingpoint numbers; often the
numerical value of a floatingpoint number does not match it's
floatingpoint value, so 45.99420 might actually count 10 decimal places
because it is actually stored as 45.9941789301 or something.)


The original question was counting digits after the decimal point
entered by the user. Any input by the user is text. The program can
simply capture the text appropriately, and count the digits the user
typed. This is not a floating point problem (as originally stated)!

If you must convert the input text to a floating point value and then
determine from that what the user typed, I think you are lost.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Feb 15 '06 #10

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

Similar topics

6
10555
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
4242
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
9589
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
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10050
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...
0
8876
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
7413
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
6675
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();...
0
5310
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3967
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
2815
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.