An Introduction to C: HW 2

The written portion of this assignment should be written in a text file — not a Word® file or a rich text file, but a basic ASCII text file, regardless of OS.

For the programming section, each question should be answered in its own file.

This assignment is due Wednesday, 28 July 2010 by 11:59 PM. Read the HW submission instructions for directions on how to submit your homework electronically.

Program 1

Write a for loop that prints the multiples of 50 between 0 and 500, inclusive.

Program 2

Write the while equivalent of program 1.

Program 3

Write a program that prompts the user for input in the form of an integer, adds up all the numbers between 1 and the user's input, then prints to the screen the total. For example, if I enter the number 12, the program prints 78.

Program 4

Write a program that prompts the user to input a number that represents seconds, then prints to the screen that number's representation in hours, minutes, and seconds. For example

$ Enter seconds:
$ 3902
$ 3902 seconds represents 1 hour, 5 minutes, and 2 seconds

Remember to handle negative numbers, as there is no such thing as –30 seconds.

Program 5

Write a program that prints a diamond on the screen according to user input. For example, if the user enters 5, the diamond will look like

  *
 ***
*****
 ***
  *

And, if the user enters 8, the diamond will look like

        * 
       * * 
     * * * * 
   * * * * * * 
 * * * * * * * * 
   * * * * * * 
     * * * * 
       * * 
        *

You may use printf statements that print either a single asterisk (*), a single blank space, or a combination of the two. Maximize your use of repetition (with nested for statements, for example) and minimize the number of printf statements.

Hint: The approach to printing odd and even diamonds is different. To get an idea of what I'm talking about, take note of the width and the height of each diamond above: 5 is the height and width of the odd diamond, while 9—an odd number— is the height and width of the even diamond. Solve the case for odd diamonds first (eg, user enters 9, 7, 3, etc), then move on to the even case (eg, user enters 22, 10, 4, etc).