Computer Science 2611
Summer 2000
Laboratory Assignment 1

Date assigned: 6/22/00

Terminology

For this assignment all numbers are positive integers.

Number k is a proper divisor of number n if k < n and k is a divisor of n. For example, 3 is a proper divisor of 6 and 5 is a proper divisor of 15. The number 1 is a proper divisor of all numbers that are greater than 1.

A number n is perfect if the sum of the proper divisors of n is n. For example, the proper divisors of 6 are 1, 2 and 3. Since 1+2+3 = 6, 6 is a perfect number. Similarly, the proper divisors of 28 are 1, 2, 4, 7, and 14. Since 1+2+4+7+14 = 28, 28 is a perfect number.

Assignment

The C++ program at the bottom of this assignment is a start at a program that reads a number n from the user and tells whether n is perfect. The program below does not work, and needs to be completed. You can compile it and run it as it stands, but it does not give the correct answers.

You need to write a loop that accumulates the sum of the proper divisors of n. Look at each number from 1 to n-1. For each one, if it is a divisor of n, add it into the sum. In a C++ program you can test whether k is a divisor of n by testing whether n%k == 0. Be sure to use a double-equal.

Test your program. If it does not work, fix it. When it works, print it and hand it in. Be sure that your name is typed in your program as a comment.

Starting point

Modify the following program.
// Your name here
// CSCI 2611 programming assignment 1

//----------------------------------------------------------
// This program reads a number from the user and tells the
// user whether that number is perfect.   
//
// A number n is perfect if the sum of the proper divisors of n is n.  
// For example, 6 is perfect since the proper divisors of 6
// are 1, 2 and 3, and 6 = 1 + 2 + 3.
//-----------------------------------------------------------

int main()
{
  int n,k,sum;

  // Read the number.

  cout << "What number should I test?" << endl;
  cin >> n;

  // Test whether n is perfect.

  sum = 0;
  k = 1;
  
  -- You will need to supply this part.  Erase these three lines and write
  -- loop that accumulates, in variable sum, the sum of the proper
  -- divisors of n.  Use k as a counter.

  // Report the answer.

  if(sum == n) {
    cout << n << " is perfect" << endl;
  }
  else {
    cout << n << " is not perfect" << endl;
  }

  return 0;
}