473,378 Members | 1,512 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

how can i count the digits of number given by the user

how can i count the digits of number given by the user
Nov 22 '07 #1
8 4073
Meetee
931 Expert Mod 512MB
how can i count the digits of number given by the user
You can take the value in string and can count by counting each char of that string until the end of the string. Search google for more. And if doubt, paste your efforts in the next post.

Regards
Nov 22 '07 #2
You can take the value in string and can count by counting each char of that string until the end of the string. Search google for more. And if doubt, paste your efforts in the next post.

Regards
Above solution is good. But if you some computing you can do as following pseudo code
Expand|Select|Wrap|Line Numbers
  1. number = user input
  2. numberDigits = 1
  3. place = 10
  4. while number >= place
  5. {
  6. numberDigits++
  7. place *= 10
  8. }
  9.  
output number
Nov 22 '07 #3
mailavi
13
Above solution is good. But if you some computing you can do as following pseudo code

number = user input
numberDigits = 1
place = 10
while number >= place
{
numberDigits++
place *= 10
}

output number

there is another way....... first u take the number from the user into a variable then divide the number with 10 u would get the unit's place digit the so on to get the individual number and add them........
Nov 29 '07 #4
Ciao Bettina,
please prompt, the user for a string ,not an integer;and then take a look to the ANSI C header <string.h> ,there is the function strlen wich counts the number of characters in a string,that could be useful!
Nov 29 '07 #5
oler1s
671 Expert 512MB
Simpler method:

Take input into a string. Convert that string into a number. Then number of digits is log(number)+1.
Nov 29 '07 #6
Ganon11
3,652 Expert 2GB
Simpler method:

Take input into a string. Convert that string into a number. Then number of digits is log(number)+1.
Just to clarify, this is log with base 10. I'm not sure if the log function in cmath evaluates log base 10, log base 2, or natural log (log base e). To make sure, use log(number)/log(desired base). No matter what base the log function is, dividing by log(desired base) results in the same answer as a log to the correct base of number.
Nov 29 '07 #7
oler1s
671 Expert 512MB
There's a both a log (natural log) and log10 (base 10 log) function in the math library.
Nov 29 '07 #8
according to compilers,there can be different implementation of the logarithm function;but basically log is computed dividing the input iteratively by ten,checking if it is >0 and incrementing a counter;we know that a microprocessor performs division through a series of right shifts;so computing the logarithm consist basically of shifting,comparing and incrementing registers;strlen only perfrom a comparison and increment a counter,without stressing ALU with shifts;though strlen algorithm has linear complexity,so it is not a big advantage to use it when the string is "long"
Nov 29 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: mark | last post by:
How to extract digits from a given number say double number is 10.9876 extract two digits after decimal - 98 extract three digits after decimal - 987 and represent the extracted number as...
1
by: Shreyas Kulkarni | last post by:
hi there, recently i have got a problem regarding calculation of sum of digits in a floating point or precision number. the weird behaviour of compiler/language is preventing me from calculating...
27
by: Luke Wu | last post by:
Is there a C function that returns the number of digits in an input int/long? example: numdigits(123) returns 3 numdigits(1232132) returns 7
68
by: Martin Joergensen | last post by:
Hi, I have some files which has the following content: 0 0 0 0 0 0 0 1 1 1 1 0 0 1 1 1 1 0 0 1 1 1 1 0 0 1 1 1 1 0 0 0 0 0 0 0
109
by: jmcgill | last post by:
Hello. Is there a method for computing the number of digits, in a given numeric base, of N factorial, without actually computing the factorial? For example, 8! has 5 digits in base 10; 10! has...
1
by: psbasha | last post by:
Hi, How to find the number of digits from a given number?. For example num = 23456 The number of digits are 5 num = 2311150
2
by: Smokey Grindle | last post by:
any fast way to do this? I just need to check if if a string which could have a lot of characters in it if it has numbers and count the number of numbers in the string. thanks!
4
by: gaurav1983 | last post by:
i have to generate unique combinations of given number of digits entered by user eg: N=4 (0,1,2,3) output should be 0 1 2 3
1
by: jlt206 | last post by:
This code <?php include("counter.php")?> on the webpage produces the count number. (function code below) I want to place the current number into a variable $MemberNo or into a FormField to be sent...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.