CSC133 Introduction to CS Using C++
Lab 7

Part 1

Write a program that reads in a number and prints a message telling whether the number is prime or not. Recall that a prime number is divisible only by itself and 1. So to determine whether a number N is prime, you need to check whether any number smaller than N (other than 1) divides N evenly. For example, if N is 20, you need to check whether 2, 3, ... , 19 divide 20 evenly. If one of them does, then N is not prime. (Can you tell if the number is prime without checking all numbers up to N - 1? If you want, see if you can do it more efficiently.)

So a sample execution of your program might look like:

Enter a number: 61
61 is a prime number

Another sample execution of your program might look like:

Enter a number: 4320
4320 is not a prime number

To write this program you will create the following functions:

Part 2

Modify your program from Part I so that it prompts the user for a number, and then prints all prime numbers less than or equal to the number entered. For example, if the user enters 20, the program will print:

Enter a number: 20
The prime numbers less than 20 are:
    2
    3
    5
    7
    11
    13
    17
    19

To write this program you will create the following functions:



Email Me | Office Hours | My Home Page | Department Home | MCC Home Page

© Copyright Emmi Schatz 2006