PDA

Click to See Complete Forum and Search --> : Number of digits


zeidhaddadin
Nov 18th, 2006, 06:55 AM
Hi all,

I have a program and I should enter an intger number, How do I check if this number is a ( 5 digits number )..

using the mod or "/" operators..

Thanks in advance,
zeid

triggernum5
Nov 18th, 2006, 08:38 AM
expo = 1
donextexpo = true
while donextexpo
'If a num1 Mod num2 doesn't change num1 then num1 < num2.. If num1 Mod num2 = 0 then num1 = integer*num2.. Since we know num1 <= num2 in our scenario then the only possible value integer is 1
if (x mod 10^expo) = x then
if x = 10^expo then 0 = expo+1
donextexpo = false
endif
expo=expo+1
wend
numdigits = ???

'--------------------------
expo = 1
donextexpo = true
while donextexpo
if x div 10^expo < 1 then donextexpo = false
expo = expo+1
wend
numdigits = ???

There are many different possible implementations using div/mod.. These functions are VITAL to be comfortable with in many types of code.. I highly advise fully understanding this rather than just solving my simple ommisions and handing it in.. I'm purposely leaving code tags out to force you to look through it closer and there is a glaringly hilarious error in there:)..

zeidhaddadin
Nov 18th, 2006, 09:00 AM
isn't there any simpler way?

Pradeep1210
Nov 18th, 2006, 09:01 AM
Private Sub Command1_Click()
If IsNumeric(Text1.Text) Then
If Val(Text1) = Int(Text1) And Len("" & Val(Text1)) = 5 Then
MsgBox "5 digit number"
Else
MsgBox "not a 5 digit number"
End If
Else
MsgBox "not a number"
End If
End Sub


Pradeep :)

zeidhaddadin
Nov 18th, 2006, 09:26 AM
I'm using java ... but I should do the calculation using mathmatical operations

triggernum5
Nov 18th, 2006, 10:22 AM
My second example is about as simple as it gets I think..

Harsh Gupta
Nov 18th, 2006, 11:02 AM
(Typecast_to_Integer) log10(number) + 1

or if log10 is not available

(Typecast_to_Integer) (log(number)/log(10)) + 1

triggernum5
Nov 18th, 2006, 11:14 AM
The (typecast_to_inter) in vb is Int() btw, and Log() is Log10.. (In vb you must understand log laws to convert to another base using Log10..)
numdigits=Int(Log(x))+1
If you can use any math functions then I take back what I said about my second method..

Harsh Gupta
Nov 18th, 2006, 02:03 PM
The (typecast_to_inter) in vb is Int() btw, and Log() is Log10.. (In vb you must understand log laws to convert to another base using Log10..)
numdigits=Int(Log(x))+1
If you can use any math functions then I take back what I said about my second method..
This is a Math forum and OP never said he wants answer in VB.

Secondly, you are mistaken about Int() in VB, it's CInt (Convert expression to Integer type) as opposed to Int (Return the Integer portion of a number).

Even in VB, Math.Log() returns you the Natural logarithm of a number, means to the base of e, and not the base of 10.

jeroen79
Nov 19th, 2006, 04:20 PM
If num >= 10000 Then is5digit

DomoCobra
Nov 20th, 2006, 04:18 AM
i think u would need if Num>=10000 and if Num<=-10000

zeidhaddadin
Nov 20th, 2006, 04:26 AM
Thanks any way .. I figured it like this:

if num >= 10,000 and num < 100,000
:)

jeroen79
Nov 20th, 2006, 05:30 AM
That and should be an or.

triggernum5
Nov 20th, 2006, 06:07 AM
Umm, I think you're looking at that boolean wrong jeroen.. Oh, and sorry about my Log base goof for vb.. Just out of curiousity, are you allowed to use ANY mathematical method? Or only div/mod?

jeroen79
Nov 20th, 2006, 07:07 AM
I meant domocobra's.

But zeidhaddadin is right.