| 
  • If you are citizen of an European Union member nation, you may not use this service unless you are at least 16 years old.

  • Stop wasting time looking for files and revisions! Dokkio, a new product from the PBworks team, integrates and organizes your Drive, Dropbox, Box, Slack and Gmail files. Sign up for free.

View
 

Exercise 14_4 Solution

Page history last edited by Steve Sweeney 11 years ago

% Input an integer value and determine if it is a prime number. 

% A prime number is a number that is divisible only by one 

% and itself.

var num1, remainder : int

var prime : boolean     % a data type for true/false values

% ask the user for a positive integer

put "Please enter a positive integer value: "..

get num1

prime := true   % start off assuming a prime number

 

% is the value is prime? - only 1 and N divide evenly 

% brute force - try dividing all numbers between 2 and (N-1)

% e.g., to see if 5 is prime, divide it by 2, 3, 4 and

% see if there is a remainder - a prime will always have one!

for count : 2 .. (num1 - 1)

    remainder := num1 mod count

    if (remainder = 0) then

        prime := false      % a zero remainder means not prime

    end if

end for

 

% output the result

if (prime = true) then

    put num1, " is a prime number"

else

    put num1, " is NOT a prime number"

end if

Comments (0)

You don't have permission to comment on this page.