Java
UNIT CHALLENGES

separator-blank.png

CHALLENGE


separator-blank.png

CHALLENGE 3 – PRIME TIME

Write a program that asks the user for a number and outputs whether or not it is a prime number.


HINTS

·         Recall that a prime number is a number that is only divisible by itself and by 1. 

·         We can check if number n is divisible by number d by using:

            if (n % d == 0)

·         For n to be prime, we need to check all numbers between 1 and n to see if they can divide n.  So, we need to loop from 2 to n-1 (or from n-1 down to 2).

PSEUDOCODE

Here is a possible pseudocode to create your program.

     Get number n from user.
     Set variable factorsFound to zero.

     Loop so d takes on all numbers from 2 to n-1
     {
          if n can be divided by d
          {
               Increment factorsFound by 1
          }
     }

     If factorFound is zero
     {
          Output: It's a prime number!
     }
     Else
     {
          Output: It's not a prime number!
     }


separator-campeau.png