|
|||||||
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
#1 |
|
Member (9 bit)
Join Date: Nov 2002
Posts: 502
|
Algorithm
How can I create an algorithm that displays if a number is divisible by 2?
|
|
|
|
|
|
#2 |
|
Member (10 bit)
Join Date: Jan 2002
Location: Edmonton, AB, Canada
Posts: 628
|
What programming language are you using?
You could set up the input so that it then divides the input by 2 then looks to see if the answer is an integer or not, an integer would mean it is divisible and a non-integer would mean otherwise. A fancier way of testing would be to just look at the digit in the one's column of the number 0,2,4,6,8 all mean it is divisible by two, the rest of the numbers do not matter, and numbers after the decimal automatically rule out whether or not they are divisible. For the actual algorithm you need to supply more information, such as what context the question is being asked and what you are going to use to program it. |
|
|
|
|
|
#3 |
|
Member (9 bit)
Join Date: Nov 2002
Posts: 502
|
The question could be, is this number divisible by 2? and the output yes it is or no it si not, the language is Pascal 7.0, it would help in visual basic too, this is for eduacational porpuses.
|
|
|
|
|
|
#4 |
|
Member (9 bit)
Join Date: Nov 2002
Posts: 502
|
Any ideas for doing this in Visual Basic?
|
|
|
|
|
|
#5 |
|
Registered User
Join Date: Nov 2001
Posts: 1,965
|
Well, I don't have VB around, and I haven't done anything in it in ages, but I can help you in Pascal:
Code:
var i : integer;
begin
Readln(i);
if ((i mod 2) = 0) then
Writeln('i is even')
else
Writeln('i is odd')
end.
Last edited by aym; 06-08-2004 at 06:01 PM. |
|
|
|
|
|
#6 | ||
|
Member (9 bit)
|
I haven't done VB since I started c++ and java 2 years ago, but this SHOULD do the trick:
Code:
Public function Div_2(i as integer) as boolean
If i mod 2 = 0 then
div_2 = true
else
div_2 = False
end if
End Function
Dave
__________________
Last edited by daveyp225; 06-08-2004 at 06:12 PM. |
||
|
|
|
|
|
#7 |
|
Member (9 bit)
Join Date: Nov 2002
Posts: 502
|
I did this into an access form and it seems to work, please check it out:
Private Sub cmdTest_Click() C = txtA / txtdiv If C = Int(txtA / txtdiv) Then MsgBox "Es divisor ", vbExclamation, C Else MsgBox "No es divisor", vbCritical, C End If End Sub What I donīt know is why do I have to enter the formula again after the Int function. |
|
|
|
|
|
#8 |
|
Professional gadfly
|
Here's VB
Code:
Public Sub Even(x as Integer)
If (x Mod 2)=0 then
Debug.Print x & " is even"
Else
Debug.Print x & " is odd"
End if
End Sub
Code:
Public Function Even(x as Integer) as Boolean Even = ((x Mod 2) = 0) End Function |
|
|
|
|
|
#9 |
|
Member (9 bit)
Join Date: Nov 2002
Posts: 502
|
Ok I typed and compiled the code in Pascal 7.0, but how can I create an exe from this source?
|
|
|
|
|
|
#10 |
|
Registered User
Join Date: Nov 2001
Posts: 1,965
|
Search the menus for "Build" or "Run", select it and a .exe will be created.
|
|
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|