Go Back   PCMech Forums > Help & Discussion > Web Design / Development

Need Some Help? Type Your Keywords Here:

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Old 06-08-2004, 12:53 PM   #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?
easg is offline   Reply With Quote
Old 06-08-2004, 01:22 PM   #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.
Trent Steel is offline   Reply With Quote
Old 06-08-2004, 01:28 PM   #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.
easg is offline   Reply With Quote
Old 06-08-2004, 04:40 PM   #4
Member (9 bit)
 
Join Date: Nov 2002
Posts: 502
Any ideas for doing this in Visual Basic?
easg is offline   Reply With Quote
Old 06-08-2004, 05:59 PM   #5
aym
Registered User
 
aym's Avatar
 
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.
aym is offline   Reply With Quote
Old 06-08-2004, 06:08 PM   #6
Member (9 bit)
 
Join Date: Mar 2004
Posts: 313
Send a message via AIM to daveyp225
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
EDIT: This should return true if the number is divisible by 2. If it doesn;t work its probably a syntax error, I'm not too familiar with VB's syntax anymore.

Dave
__________________

Thermaltake Swing | 550W Corsair PSU | Asus P5K DLX/WiFi | Intel C2D E8400 @ (???)ghz | 2x2GB G.Skill DDR2-1000 | eVGA 8800GT Superclocked | 400GB Seagate 7200.11 (32mb) | 20x ASUS DVD-RW with Lightscribe | 20x Samsung DVD-RW | Black Floppy (just in case) | 52-in-1 Card Reader | UV lights & cables


Enermax CS-800TA | 400Watt Antec PSU | Asus P4c800 Deluxe | P4 2.8c @ 3.5ghz | 2x512 KHX pc4000 | ATI 9800pro AIW 128mb | 120GB 7200RPM WD Hard Drive | 8x NEC DVDRW | 16x sony dvd-rom | sony silver floppy


Last edited by daveyp225; 06-08-2004 at 06:12 PM.
daveyp225 is offline   Reply With Quote
Old 06-09-2004, 11:15 AM   #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.
easg is offline   Reply With Quote
Old 06-09-2004, 11:36 AM   #8
Professional gadfly
 
doctorgonzo's Avatar
 
Join Date: Jan 2002
Location: Minneapolis, MN
Posts: 6,364
Send a message via MSN to doctorgonzo
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
Replace the Debug.Print with whatever output device you want to use. You can also turn it into a function like this, which is even simpler:

Code:
Public Function Even(x as Integer) as Boolean
  Even = ((x Mod 2) = 0)
End Function
doctorgonzo is offline   Reply With Quote
Old 06-09-2004, 06:32 PM   #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?
easg is offline   Reply With Quote
Old 06-10-2004, 06:40 PM   #10
aym
Registered User
 
aym's Avatar
 
Join Date: Nov 2001
Posts: 1,965
Search the menus for "Build" or "Run", select it and a .exe will be created.
aym is offline   Reply With Quote
Reply

Bookmarks

Still Need Help? Type Your Keywords Here:


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 06:47 AM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
SEO by vBSEO 3.6.0 PL2