Introduction to Computer Science

CSCI 151 Introduction to Computer Science
Assignment 4

The due date is Monday, December 7. The printout of the programs should be submitted in the lab, and the e-mail with the programs should be received by 3:50 pm on Monday, December 7, 2009

NO LATE SUBMISSIONS!!!

Do your own work. Programs which are written in groups are easily identified and will receive grades of 0 for all participants.

Skill set for this assignment:

  • Use if, if-else, if - else - if, switch, while and for statements; int, double and float variable types.
  • Use an appropriate variable types and casting
  • Understand and implement the Program Development Cycle
Problem 1 (20 points)
Write a program that reads 7 real numbers. Each number indicates the Fahrenheit temperature for one day. The program finds and prints the average Celsius temperature for these 7 days.
Use the following formula to convert Fahrenheit to Celsius: Celsius = (5/9)*(Fahrenheit-32)

Example:

Input: 44.76 -67 87.5 0 12 86.89 90

Output: The average Celsius temperature is 2.392857

Problem 2 (20 points):
Write a program that inputs a series of non-negative integers between 0 and 100. Each integer indicates the student's grade. The first negative value will terminate the input. The program outputs the letter equivalent for each numeric grade according to the following table:
A: 100 - 90
B: 89 - 75
C: 74 - 65
D: 64 - 60
F: 59 - 0
You can assume that the input is a valid sequence of integers between 0 and 100. You can assume that the input is not empty - there is at least one non-negative integer in the input sequence before the negative number that terminates the input. You don't need to check the validity of the input.

Example 1:
Input: 63, 75, 100, 0, 74, 86, 98, 75, -9
Output: D, B, A, F, C, B, A, B

Example 2:
Input: 75, -8
Output: B

Problem 3 (10 points):
Write a C program that prompts its user for a nonnegative value n. The program then displays as its output:

1 2 3 ... n-1 n
1 2 3 ... n-1
...
1 2 3
1 2
1

Program 4 (20 points)
Write a program that reads one integer that indicates the numeric value of the month and one integer that indicates the year. Your program will print the amount of days in the month that was entered. For example, if the input was 5 ( May) the program will output 30 days, if the input was 12 ( December), the program will output 31 days, if the input is 2 ( February), the program will check the year and find out if the amount of days is equal 28 or 29 (the algorithm for calculating a leap year is as follows: A year will be a leap year if it is divisible by 4 but not by 100. If a year is divisible by 4 and by 100, it is not a leap year unless it is also divisible by 400. Thus years such as 1996, 1992, 1988 and so on are leap years because they are divisible by 4 but not by 100. For century years, the 400 rule is important. Thus, century years 1900, 1800 and 1700 while all still divisible by 4 are also exactly divisible by 100. As they are not further divisible by 400, they are not leap years), if the input is less or equal 0 or greater than 12, the program will print the message "Invalid month".
You have to use SWITCH statement in this problem

Problem 5 (10 points)
There is a treasure box with 1 million dollars. The chest has a 3-digit combination lock that opens under the following conditions: the first digit should be equal to the last digit, the second digit should be even, and the sum of all digits should be divisible by 4. Write a program that reads a series of non-negative integers between 100 and 999. The first negative value will terminate the input. For each input, the program checks if the number opens the chest or not. For each input, the output should be YES, if the chest opens, and NO otherwise.

Example: 525 425 515 505 -1
Output:
YES NO NO NO

Problem 6 (20 points)
Write a program which will find and display ALL four-digit positive integers that are divisible by the sum of their digits. For example, number 1100 is divisible by sum of its digits.

Bonus Question 1 (if you didn't submit it last time) (10 points)

The ancient Greeks were fascinated with numbers that are equal to the sum of their proper factors; they called such numbers perfect. For example, 6 is perfect because its proper factors (i.e. numbers that are less than 6 and divide 6 evenly) are 1, 2 and 3 with the sum being equal to 6. Another perfect number is 28, since 28 = 1+2+4+7+14 (verify manually that 28 only has these proper factors).

Write a C program that reads in a number N and prints out all those numbers between 1 and N that are perfect numbers. For example, if the prompted input was 30, the program would print out 6 and 28. Your program should be able to accept values of N up to 10000.

The program has to check a validity for the input.

A few remarks and helpful hints:

  • Note that 1 is always included in the sum of proper factors.
  • Use a for loop with index variable i, say, to iterate from 1 to N; within the loop, your program should determine if i is perfect. It will do so by using another loop (nested within the outer loop) in which you should systematically account for all potential proper factors of i.
  • Don't bother trying to do this by hand: there are only 4 perfect numbers between 1 and 10000. This rarity was no doubt part of the appeal for the Greeks!
  • If P divides i evenly, P is greater than 1 and P is less than i, then i/P is also a proper factor of i. For instance, consider i=192 and P=12. P divides i evenly since 12*16=192. But this gives us another factor, 192/12 = 16.

Bonus Question 2 (10 points)

Write a C program that reads one integer. If the integer is zero or negative the program prints an error message, if the input number is positive the program prints all prime numbers between 1 and the input number (include the input number into your range).

For example, if the input is 13, the output should be
2, 3, 5, 7, 11, 13

For example, if the input number is 21, the output should be
2, 3, 5, 7, 11, 13, 17, 19