|
-
Nov 18th, 2006, 07:55 AM
#1
Thread Starter
Hyperactive Member
Number of digits
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
-
Nov 18th, 2006, 09:38 AM
#2
Hyperactive Member
Re: Number of digits
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 ..
Last edited by triggernum5; Nov 18th, 2006 at 09:43 AM.
-
Nov 18th, 2006, 10:00 AM
#3
Thread Starter
Hyperactive Member
Re: Number of digits
isn't there any simpler way?
-
Nov 18th, 2006, 10:01 AM
#4
Re: Number of digits
VB Code:
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
-
Nov 18th, 2006, 10:26 AM
#5
Thread Starter
Hyperactive Member
Re: Number of digits
I'm using java ... but I should do the calculation using mathmatical operations
-
Nov 18th, 2006, 11:22 AM
#6
Hyperactive Member
Re: Number of digits
My second example is about as simple as it gets I think..
-
Nov 18th, 2006, 12:02 PM
#7
Re: Number of digits
Code:
(Typecast_to_Integer) log10(number) + 1
or if log10 is not available
(Typecast_to_Integer) (log(number)/log(10)) + 1
-
Nov 18th, 2006, 12:14 PM
#8
Hyperactive Member
Re: Number of digits
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..
-
Nov 18th, 2006, 03:03 PM
#9
Re: Number of digits
 Originally Posted by triggernum5
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.
-
Nov 19th, 2006, 05:20 PM
#10
Frenzied Member
Re: Number of digits
If num >= 10000 Then is5digit
-
Nov 20th, 2006, 05:18 AM
#11
Member
Re: Number of digits
i think u would need if Num>=10000 and if Num<=-10000
-
Nov 20th, 2006, 05:26 AM
#12
Thread Starter
Hyperactive Member
Re: Number of digits
Thanks any way .. I figured it like this:
if num >= 10,000 and num < 100,000
-
Nov 20th, 2006, 06:30 AM
#13
Frenzied Member
Re: Number of digits
That and should be an or.
-
Nov 20th, 2006, 07:07 AM
#14
Hyperactive Member
Re: Number of digits
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?
-
Nov 20th, 2006, 08:07 AM
#15
Frenzied Member
Re: Number of digits
I meant domocobra's.
But zeidhaddadin is right.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|