444,225 Members | 2,223 Online Need help? Post your question and get tips & solutions from a community of 444,225 IT Pros & Developers. It's quick & easy.

# Digits after the decimal point

 P: n/a I am doing Engineering(B.Tech) 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 Replies

 P: n/a Kuljit wrote: I am doing Engineering(B.Tech) 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) 2. Use scanf(hint: scanf("%s", input)) 3. Start walking the string character by character(assumes 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

 P: n/a "Kuljit" writes: I am doing Engineering(B.Tech) 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

 P: n/a "osmium" writes: Never mind. Wrong news froup. Feb 15 '06 #4

 P: n/a gamehack wrote: Kuljit wrote: I am doing Engineering(B.Tech) 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) 2. Use scanf(hint: scanf("%s", input)) 3. Start walking the string character by character(assumes 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.984 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

 P: n/a Nelu wrote: gamehack wrote: Kuljit wrote: I am doing Engineering(B.Tech) 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) 2. Use scanf(hint: scanf("%s", input)) 3. Start walking the string character by character(assumes 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.984 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

 P: n/a -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Kuljit wrote: I am doing Engineering(B.Tech) 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 iD8DBQFD825MagVFX4UWr64RAqypAJ9Ly9NJPaw4IA6cyVJV+v uyk5eZxACeMQf0 qnivmuy3/BTPu8WsLeK0+9o= =FNJR -----END PGP SIGNATURE----- Feb 15 '06 #7

 P: n/a Nelu schrieb: Nelu wrote:gamehack wrote:Kuljit wrote:I am doing Engineering(B.Tech) 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 thenumber 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 countthe number of digits in that number after the decimal point(4 in thiscase).eg: If user enters 45.99420 then we should get 5, which are the numberof digits after the decimal point.- KuljitWell...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)2. Use scanf(hint: scanf("%s", input))3. Start walking the string character by character(assumes ASCII)4. When you encounter the char x = '.' start increment a counter5. 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 actualvalid number, i.e.:input like +a1b2.53.678.984 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

 P: n/a 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

 P: n/a Lew Pitcher wrote: -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Kuljit wrote:I am doing Engineering(B.Tech) 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 thenumber 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 countthe number of digits in that number after the decimal point(4 in thiscase).eg: If user enters 45.99420 then we should get 5, which are the numberof 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

 P: n/a gamehack wrote: Kuljit wrote: I am doing Engineering(B.Tech) 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) 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(assumes 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

 P: n/a 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) 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(assumes ASCII) You don't have to assume ASCII to walk a string character by character. Feb 15 '06 #12

 P: n/a On 15 Feb 2006 10:03:18 -0800, "gamehack" wrote: Kuljit wrote: I am doing Engineering(B.Tech) 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. - KuljitWell...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)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(assumes ASCII)4. When you encounter the char x = '.' start increment a counter5. Finish incrementing when you've reached '\0'Hope you can write the code yourself. Remove del for email Feb 17 '06 #13

 P: n/a 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

 P: n/a 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): ::= . ::= []['.' []]. ::= 'e' | 'E' | 'd' | 'D'. ::= []. ::= '+' | '-'. ::= []*. ::= '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

 P: n/a 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): ::= . ::= []['.' []]. ::= 'e' | 'E' | 'd' | 'D'. ::= []. ::= '+' | '-'. ::= []*. ::= '0' ... '9'. Obviously: ::= '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

 P: n/a 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 