ALGORITHMS

An algorithm is a finite series of steps that will arrive at a desired solution or accomplish a given task. A kitchen recipe is an example of an algorithm, and so is a series of assembly instructions that comes packaged in a toy or other object that is not already assembled when you buy it.

A program is a series of instructions to a computer. It is therefore a type of algorithm, written in a particular computer language. The program could be rewritten in a different language but still use the same algorithm.

In order to demonstrate an algorithm and how it relates to a program, here is an algorithm and its implementation as a program. The algorithm simply determines whether an integer is even or odd.



EVEN OR ODD?

Determining whether an integer number is even or odd is equivalent to determining whether or not it is divisible by 2. A number n is divisible by 2 if n/2 is an integer.


ALGORITHM FOR DETERMINING EVEN OR ODD:
  1. Ask for an integer.
  2. Set N equal to the integer typed in.
  3. Set H equal to N/2.
  4. If H is an integer, go to step 7. (If not, proceed to step 5.)
  5. Print "That's an odd number."
  6. Skip to the end (step 8).
  7. Print "That's an even number."
  8. End of procedure.


PROGRAM FOR DETERMINING EVEN OR ODD:
This program is written in BASIC using the above algorithm.
It uses the INT function in BASIC, which evaluates the integer part of any number, ignoring the fractional part. (This function is further explained if you click here).


 110 PRINT "TYPE IN AN INTEGER:"
 120 INPUT N
 130 LET H=N/2
 140 IF H=INT(H) THEN 170
 150 PRINT "That's an odd number."
 160 GO TO 199
 170 PRINT "That's an even number."
 199 END  



CIS 101 home A: drive