Ask Experts Questions for FREE Help!
 

Free Answers in 3 Easy Steps

Register Now
3 Steps
 


Ask QuestionsprogressAnswer QuestionsprogressBuild ReputationprogressBecome an Expert
 
At Ask Me Help Desk you can ask questions in any topic and have them answered for free by our experts. To ask questions or participate in answering them you must register for a free account. By registering you will be able to:
  • Get free answers from experts in any of our 300+ topics.
  • Accept money for answers that you provide.
  • Communicate privately with other members (PM).
  • See fewer ads.
  Answer this Question    Ask about Visual Basic    Ask about another Subject  
 

peter1988
Sep 28, 2009, 05:55 AM
may anyone tell me how to do this calculation,the code into Visual Basic?
(Question)
if the user inputs 15 and 3, the first number is a multiple of the second. If the user inputs 2 and 4, the first number is not a multiple of the second.

Perito
Sep 28, 2009, 11:21 AM
I'm not sure I understand the question.

Is this the question? how can I determine if the second number is an integral multiple of the first? If that is the question, this is how to do it (This is a VB6 program)

Dim FirstValue As Double
Dim SecondValue As Double

FirstValue = 15
SecondValue = 3

If (CInt(FirstValue / SecondValue) * SecondValue = FirstValue) Then
Call MsgBox("The second value is an integral multiple of the first", vbInformation)
Else
Call MsgBox("The second value is NOT an integral multiple of the first", vbInformation)
End If

You can also do it this way (using the "\" operator, the integral division operator).

If ((FirstValue \ SecondValue) * SecondValue = FirstValue) Then
Call MsgBox("The second value is an integral multiple of the first", vbInformation)
Else
Call MsgBox("The second value is NOT an integral multiple of the first", vbInformation)
End If

The key is the "CInt" or the integer division operator "\". Other languages have other functions to do the same thing (Trunc, Fix, IFIX, etc. VB has the FIX operator.) Basically, you throw away the fractional part (or the remainder). If you then multiply by the divisor, you should get back the original number. If you don't, it's not an integral multiple.

Note that VB is somewhat more forgiving than some other languages. In some languages. Round-off errors can make the IF statement fail incorrectly. In these cases, you would have to do something like this:

Dim TestValue as double
TestValue = ((FirestValue \ SecondValue) * SecondValue) - FirstValue

if ABS(TestValue) < 0.0000001 then
' The second value is an integral divisor
else
' The second value is NOT an integral divisor
end if