Programming Exercises

I l @ ve RuBoard

Programming Exercises

  1. Write a program that creates an array with 26 elements and stores the 26 lowercase letters in it. Also have it show the array contents.

  2. Use nested loops to produce the following pattern:

     $ $$ $$$ $$$$ $$$$$ 
  3. Use nested loops to produce the following pattern:

     F FE FED FEDC FEDCB FEDCBA 
  4. Have a program request the user enter an uppercase letter. Use nested loops to produce a pyramid pattern like this:

     A    ABA   ABCBA  ABCDCDA ABCDEDCBA 

    The pattern should extend to the character entered. For instance, the preceding pattern would result from an input value of E . Hint: Use an outer loop to handle the rows. Use three inner loops in a row, one to handle the spaces, one for printing letters in ascending order, and one for printing letters in descending order.

  5. Write a program that prints a table with each line giving an integer, its square, and its cube. Ask the user to input the lower and upper limits for the table. Use a for loop.

  6. Write a program that reads a single word into a character array and then prints the word backward. Hint: Use strlen() to compute the index of the last character in the array (refer to Chapter 4, "Character Strings and Formatted Input/Output").

  7. Write a program that requests two floating-point numbers and prints the value of their difference divided by their product. Have the program loop through pairs of input values until the user enters non-numeric input.

  8. Modify Exercise 7 so that it uses a function to return the value of the calculation.

  9. Write a program that reads eight integers into an array and then prints them in reverse order.

  10. Consider these two infinite series:

     1.0 + 1.0/2.0 + 1.0/3.0 + 1.0/4.0 + ... 1.0 - 1.0/2.0 + 1.0/3.0 - 1.0/4.0 + ... 

    Write a program that evaluates running totals of these two series up to some limit of number of terms. Have the user enter the limit interactively. Look at the running totals after 20 terms, 100 terms, 500 terms. Does either series appear to be converging to some value? Hint: -1 times itself an odd number of times is -1, and -1 times itself an even number of times is 1.

  11. Write a program that creates an eight-element array of int and sets the elements to the first eight powers of 2 and then prints the values out. Use a for loop to set the values, and, for variety, use a do while loop to display the values.

  12. Write a program that reads in a line of input and then prints the line out in reverse order. Recall that you can use scanf() with the %c specifier to read one character at a time from input and that the newline character ( \n ) is generated when you press the Enter key.

  13. Daphne invests $100 at 10% simple interest. (That is, every year, the investment earns an interest equal to 10% of the original investment.) Deirdre invests $100 at 5% interest compounded annually. (That is, interest is 5% of the current balance, including previous addition of interest.) Write a program that finds how many years it takes for the value of Deirdre's investment to exceed the value of Daphne's investment. Also show the two values at that time.

  14. Chuckie Lucky won a million dollars, which he places in an account that earns 8% a year. On the last day of each year, Chuckie withdraws $100,000. Write a program that finds out how many years it takes for Chuckie to empty his account.

I l @ ve RuBoard


C++ Primer Plus
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2000
Pages: 314
Authors: Stephen Prata

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net