Assignment #1

Due: Thursday, May 25

Objective

This assignment will consist of writing two small programs, each giving practice with basic I/O (printf/scanf), involving text and integer variables and operations.

Task

Write the following programs, each in a separate file. Filenames should be: (Note that the filenames are all lowercase)

Exercise 1

Write a program (filename rectangle.c) that does the following:

Prompt the user to type in the length of a rectangle, then the width (you may assume both of these will be integers). Then compute and print out the perimeter and the area of the rectangle, as shown in the examples below. Your output must match mine exactly.

Remember that for a rectangle:

Sample run 1: (the user input is underlined in the example)

Please enter the length of the rectangle: 30
Please enter the width of the rectangle: 23

For a 30 x 23 rectangle:
   perimeter = 106
   area = 690

Sample run 2:

Please enter the length of the rectangle: 100
Please enter the width of the rectangle: 5

For a 100 x 5 rectangle:
   perimeter = 210
   area = 500

Exercise 2

Write a program (filename digits.c) that does the following:

Prompt the user and let them enter a 5 digit integer. (You may assume that the entry will be a 5 digit positive integer value). Break up the value into the component digits and add the digits together. Print the digits and the addition result in the format shown below. Your output must match mine exactly.

Hint: In lecture, we talked about the / and % operators (quotient and modulus) for integer types, and some of their uses. These operators are your friends! (And they can help you split a number up into digits!)

Sample run 1: (user input is underlined in example)

Please enter a 5-digit number: 45678

The sum of the digits:
   4 + 5 + 6 + 7 + 8 = 30

Sample run 2:

Please enter a 5-digit number: 97882

The sum of the digits:
   9 + 7 + 8 + 8 + 2 = 34

Sample run 3:

Please enter a 5-digit number: 99999

The sum of the digits:
   9 + 9 + 9 + 9 + 9 = 45

Requirements for both programs


Submitting:

Program submissions should be done through the submission web page, which is linked from the main course web site. The passwords for the submission page were distributed in recitation on May 16, and you should have had the opportunity to practice with the submission page to ensure that you know how to use it. If you did not attend that class, please e-mail me ASAP to obtain your password. Do not send program submissions through e-mail -- e-mail attachments will not be accepted as valid submissions.

Submit only your source code files. As there are two of them, this will require filling out the submission page twice. Submit the files:

  rectangle.c
  digits.c