Вы находитесь на странице: 1из 3

Loops and Conditions Exercises

1.Write a program that will read in 4 numbers and print out their average.
2.Write a program that displays all multiples of 5 between 0 and 100 inclusive.
3.Write a program that calculates the sum of the square of all multiples of 3 between 0 and
100.
4.Consider the sequence 1, 2, , n. Write a program that will calculate the sum, product and
average of the numbers in the sequence. The value of n will have to be input.
5. Write a program that allows the input of an integer value n and displays all multiples of 3
which are less than or equal to n, as well as the sum of the square of these values.
6. Write a program to read in a set of numbers and print out their average. The program will
start by prompting the user for the number of numbers to be read in and will then prompt
for the individual numbers with a prompt such as
Enter Number 23

to indicate to the user which data item is currently being entered. Do something special
when prompting for the last number.
Note that there is no need to store all the individual numbers, it is sufficient to maintain a
running total.
7. Modify the previous program to print out the largest and smallest number read in as well
as the average. Also change the prompt to show the number of numbers still to be
entered.
8. Write a program to prompt the user for an integer and calculate the sum of all the integers
up to and including the input value. Print out the result.
9. Modify the previous program to use floating point arithmetic to add up the reciprocals of
all the integers up to and including the input value.
10. Further modify the previous program to print out a list of reciprocal sums for every
integer up to and including the input value.
I.e. print out the sum of the reciprocals of the integers up to and including 1, up to and
including 2, up to and including 3 etc., etc.
11. Write a program to print out the integers from 40 to 127 in decimal, octal, hexadecimal
and also print out the equivalent character.
12. Write a program that allows the entry of a list of integer values, terminated by 1 and
display the sum of the values. (Do not include the 1 in the sum).

13. Write a program that allows you to input an integer value n. If n is greater than 100, it
displays the message Wrong Input, otherwise it displays all factors of n.
14. Write a program that continually prompts the user to enter integers from the keyboard.
The program terminates when the integer entered is 5 or 0 or is greater than 8. Use
logical and in your loop control condition. Test your program carefully to ensure that all
the loop termination criteria are met.
15. The population of rabbits on a small island in Poole Harbour is being monitored. There
are currently 495 rabbits on the island. It is estimated that the population will grow at
20% a year until the population exceeds 1,000, after which the growth rate will be 10% a
year. Write a program to calculate how many years (integer value) will elapse before the
rabbit population exceeds 1,500.
[Please use integral numbers for rabbits rabbits should not have fractional parts.]
16. As a seasonal amusement some programmers like to print out a picture of a Chirstmas
Tree looking like this.
*
***
*
***
*****
*
***
*****
*******
|
---+---

The tree consists of a series of tiers (three in this case) of increasing size. Write a
program to produce such a display having prompted the user for the number of tiers.
You could try putting a few baubles on the tree (using o or O).
17. A year is a leap year if it is divisible by 4 unless it is a century year (one that ends in 00)
in which case it has to be divisible by 400. Write a program to read in a year and report
whether it is a leap year or not.
18. Write a program that reads in a sequence of positive and negative integers and prints
whether there were more positive or negative integers.
19. Write a program that reads in a sequence of positive integers and prints out the longest
streak of the same value. E.g., a sample run would be as follows:
2 6 4 3 3 2 3 2 2 2 2 6 6 6 1 6 6 6
Streak of 4 2's in a row.

20. Write a program that reads in two integers m and n, and prints out an m-by-n rectangle of
asterisks.
(a) Use two nested while loops.
(b) Use two nested for loops.

E.g., a sample run would be as follows:


Enter the height followed by the width: 3 10
**********
**********
**********

Вам также может понравиться