Homework #3

Due Friday, May 1, 11:59 PM

Answer to each question should be in a separate file. The files should not be written to after the due date.
  1. [2 points] A perfect number is a positive integer that equals the sum of its (exact) positive integer divisors, including unity (one) but excluding the number itself. The number 6 is the smallest such number because 6 = 1 + 2 + 3. Also, as a non-example, 12 is NOT a perfect number because its factors, excluding 12 itself, are 1, 2, 3, 4, and 6, and adding all these factors equals 1+2+3+4+6 = 16 which is not equal to 12. Write a program to compute and print the first four perfect numbers. You must be able to explain in great detail what the program does during the time of grading/interview with your proctor. I will be instructing proctors to ask questions such as "What happens if I change this value to this value," or "Explain how this for-loop works," or "How many times is this for-loop executed," or some similar question. Something similar MAY appear in the midterm exam. No guarantees but you never know.

    If you are having trouble with while statement, check the notes..

    There are some hints on hw#3. check out FAQs Page

  2. [1.5 points] What is the value of x at the end of the following program fragment? You should write a program to find out, and explain how it works at the time of (interview) grading of your homework. (The formatting of the program as it is presented below is intentionally unindented - however, when you write it, you should use the indenting conventions).
    int x = 8, y = 2, w = 6, z = 1; if ( y < x ) z++; if ( z == y ) x = --w; else x = w--;

  3. [1.5 points] The following declarations are given:
    int a = 11, b = 5 , c = 3 , d = 0, e = -4 ;
    float f;
    int i;
    What is the value of each of the following C language expressions when they are executed in isolation?
    f = ((float) a)/((float) b) +c
    i = (c || b && d)
    i = (d == (--a % b))

Back to Grading & Homeworks page