Dana Vrajitoru
C101 Computer Programming

Homework 8

Due date: Thursday, March 25, 2004.

Ex. 1 Write a program that inputs an integer number and decides whether it is a prime number or not. Remember that a prime number has exactly 2 divisors: 1 and itself. The numbers 0 and 1 are not prime numbers. For this, you will have to implement 2 functions:

Ex. 2 (5 extra credit points) Rewrite the function that decides if a number is prime without counting the divisors. For this function, use a for loop starting from 2 and going at most to the square root of the number until it encounters a divisor. If that divisor is equal to 1, then the number is prime (use a return statement to interrupt the loop and get out of the function). You will have to use the function sqrt from the library. See pages 136-140 from the textbook.

Note. If you turn in only one of the two exercises, it is worth 20 points.

Send me: Source code file containing an example of program execution.