L10-Ch+4 loops+Sample+Questions

Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

Chapter 4 Loops

Some sample questions


1. You put $10,000 into a bank account that earns 5 percent interest per year. Write a
program to find out: [Page 132]
a. How many years does it take for the account balance to be double the original
investment?
b. How many years does it take for the investment to triple?
c. If the interest rate is 10 percent per year, how many years does it take for the
investment to double?

2. Write programs with loops that compute [P4.1, Page 178]


a. The sum of all even numbers between 2 and 100 (inclusive).
b. The sum of all squares between 1 and 100 (inclusive).
c. All powers of 2 from 20 up to 220.
d. The sum of all odd numbers between a and b (inclusive), where a and b are
inputs.
e. The sum of all odd digits of an input. (For example, if the input is 32677, the sum
would be 3 + 7 + 7 = 17.)

3. Write programs that read a sequence of integer inputs and print [P4.2, Page 178]
a. The smallest and largest of the inputs.
b. The number of even and odd inputs.
c. Cumulative totals. For example, if the input is 1 7 2 9, the program should print 1
8 10 19.
d. All adjacent duplicates. For example, if the input is 1 3 3 4 5 5 6 6 2, the program
should print 3 5 6.

4. Write programs that read a line of input as a string and print [P4.3, Page 178]
a. Only the uppercase letters in the string.
b. Every second letter of the string.
c. The string, with all vowels replaced by an underscore.
d. The number of vowels in the string.
e. The positions of all vowels in the string.

5. Write a program that generates the following table:

--------------------- --------------------- ------------------- --------------------- ---------------------


n Count Running Sum Running Average Power of n
--------------------- --------------------- ------------------- --------------------- ---------------------
1 1 1 1 1
2 2 3 1.5 4
3 3 6 2 9
4 4 10 2.5 16
5 5 15 3 25
6 6 21 3.5 36
--------------------- --------------------- ------------------- --------------------- ---------------------

You might also like