Homework 11: Due Tuesday, October 24th

Complete the following problems:

1.  Prompt the user to enter a number from 1 to 100 (including
    1 and 100). If the user enters a number outside the range,
    keep re-prompting until a valid number is entered.
    Use a do-while loop.


2.  Write a program segment that adds all the odd numbers from 1 to N.
    N will be entered by the user, and you may assume N >= 1.
      A.  use a while loop
      B.  use a for loop
      C.  use a do-while loop


3.  The square of an integer n is normally calculated by multiplying n by itself,
    that is, n2 = n * n. Another way to calculate n2
    is by adding the first n odd integers. For example,

		1 + 3 + 5 = 9 = 32
		1 + 3 + 5 + 7 = 16 = 42
		1 + 3 + 5 + 7 + 9 = 25 = 52

    Write a program segment that uses this method to calculate and
    print the square of a number. The program should prompt the user
    to enter an integer, then use this method to calculate and then print
    the square of the integer entered. Use a do-while loop.


4.  Modify your answer to question 3. If the user enters a number less
    than 1, reprompt until a positive number is entered. Use a do-while loop.


5.  Write a program segment that reads an integer and counts and prints
    the number of digits in the integer. Use a do-while loop.


Remember to type your answers and to make two copies of each homework assignment. One copy is submitted to me and the other you keep to use in the group discussions.


Email Me | Office Hours | My Home Page | Department Home | MCC Home Page

© Copyright Emmi Schatz 2004