## count of digits in large number

General FreeBASIC programming questions.
ganache
Posts: 45
Joined: Aug 04, 2016 9:25

### count of digits in large number

here is some code works in c language

Code: Select all

`function  countDigit(n As Ulongint) As Integerreturn INT(log(10(n)) + 1)end function  Dim As Integer n input "Enter number: ";nprint countDigit(n)Sleep`

But compiler says expected '' ( " in return statement. No matter how i try to balance the brackets, it still complains.
SARG
Posts: 1157
Joined: May 27, 2005 7:15
Location: FRANCE

### Re: count of digits in large number

Hi

Change log(10(n)) by log (n)

Edit : note natural logarithm, divide by log(10) if the need is logarithm of base 10

Code: Select all

`function  countDigit(n As Ulongint) As Integerreturn INT((log(n)/log(10)) + 1)end function Dim As Integer ninput "Enter number: ";nprint countDigit(n)sleep`
fxm
Posts: 9980
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

### Re: count of digits in large number

But since the Log-based 10 built-in function does not exist in FreeBASIC, the division of Log(n) by Log(10) introduces a rounding error, and therefore the final function may have an error on the number of digit at the borders ( 9-10, 99-100, 999-1000, ...).
Provoni
Posts: 391
Joined: Jan 05, 2014 12:33
Location: Belgium

### Re: count of digits in large number

Code: Select all

`print len(str(n))`
dodicat
Posts: 6718
Joined: Jan 10, 2006 20:30
Location: Scotland

### Re: count of digits in large number

The crt gives a log10

Code: Select all

`#include "crt.bi"function  countDigit(n As Ulongint) As Integerreturn INT(log10(n) + 1)end function Dim As Integer ninput "Enter number: ";nprint countDigit(n)Sleep `
ganache
Posts: 45
Joined: Aug 04, 2016 9:25

### Re: count of digits in large number

thanks guys for the suggestions.
But it seems that the log method is incorrect. Tried it in c.
The code below works for numbers that fit into the largest numeric data type supported
by FB. After that I guess the integers start to overflow...

Code: Select all

`Dim As Ulongint numberDim As Integer countinput "Enter a integer number: ",numberwhile number > 0number /= 10count +=1wendprint "No. of digits is: ";countSleepEnd`