CS35: Assignment 15

CS35: Programming and Problem Solving
Ray Ontko
Department of Computer Science
Earlham College

Due Friday, 1999/10/22

Programming Exercise

Do one of the following exercises:

  1. A positive number is said to be perfect if it is equal to the sum of its positive factors (less than itself). For example, the factors of 6 are 1, 2, 3, and 6; 6 is perfect because 1+2+3 is equal to 6. Not however that the factors of 12 are 1, 2, 3, 4, 6, and 12; 12 is not perfect because 1+2+3+4+6 does not equal 12. Your goal is to write a program which lists all the perfect numbers between 1 and 1000. (See exercise 5.26 in Deitel and Deitel for a similar problem.)

    Your program should include a function called isperfect() which returns a 1 if the number is perfect, or 0 if it is non-perfect or non-positive. The declaration for your function should look something like this:

    int isperfect( int n ) ;
    

  2. A positive number is said to be prime if it only has as factors the number 1 and itself; no other positive number evenly divides a prime number. For example, 2, 3, 5, 7, 11, 13, 17, and 19 are prime, but 4, 6, 8, 9, 10, 12, 14, 15, 16, 18, and 20 are not. Your goal is to write a program which lists all the prime numbers between 2 and 1000. (See exercise 5.27 in Deitel and Deitel for more a similar problem.)

    Your program should include a function called isprime() which returns a 1 if the number is prime, or 0 if it is less than 2 or non-prime. The declaration for your function should look something like this:

    int isprime( int n ) ;
    

    If you use the sqrt() function, you may need to compile your program using a command like this:

    $ cc -o my_prog -lm my_prog.c
    

    The "-lm" tells the compiler to include the "mlib" library when linking your program. "mlib" is the library which contains sqrt().

Copyright © 1999, Ray Ontko (rayo@ontko.com).