Difference between revisions of "Prime numbers assignment"

From WLCS
Line 11: Line 11:
 
# Use a loop that counts from 2 up to '''N/2'''
 
# Use a loop that counts from 2 up to '''N/2'''
 
## If N is divisible by the loop counter, then set '''isPrime''' to '''False''' and '''break''' out of the loop
 
## If N is divisible by the loop counter, then set '''isPrime''' to '''False''' and '''break''' out of the loop
 +
## In order to check for divisibility, you can get the remainder by using the modulus (%) operator.  Compare the remainder to 0 to see if it is evenly divided
 
# After the loop, check if '''isPrime''' is '''True''',  
 
# After the loop, check if '''isPrime''' is '''True''',  
 
## print out that it is a prime number,  
 
## print out that it is a prime number,  

Revision as of 08:31, 28 October 2015

Objective:

  • To become well-learned in the way of the while loop

Resources:

Directions:

  1. Prompt the user to enter a number, N
  2. Create a variable named isPrime and set it to True
  3. Use a loop that counts from 2 up to N/2
    1. If N is divisible by the loop counter, then set isPrime to False and break out of the loop
    2. In order to check for divisibility, you can get the remainder by using the modulus (%) operator. Compare the remainder to 0 to see if it is evenly divided
  4. After the loop, check if isPrime is True,
    1. print out that it is a prime number,
    2. else, print out that it is not a prime number

Additional Challenges:

  1. Prompt the user for A
  2. Print out the first A prime numbers