• 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. Connect your Gmail, DriveDropbox, and Slack accounts and in less than 2 minutes, Dokkio will automatically organize all your file attachments. Learn more and claim your free account.


Exercise 14_4 Solution

Page history last edited by Steve Sweeney 11 years, 9 months 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"


    put num1, " is NOT a prime number"

end if

Comments (0)

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