Loops

advertisement
G6DICP - Introduction to Computer
Programming
Exercise Sheet 2
1) Write a program that calculates and displays the circumference and area of a
circle. You should provide it with a radius by setting that value into a variable
called “radius”. NB circumference = 2r, and area = r2, and assume that is
3.14159. Test your program with known data (ie calculate the answer yourself).
2) Write a program that creates two variables called inside_temp and outside_temp.
Set temperatures (as degrees C) into these variables, representing the
temperature inside and outside. Your program should then analyse these two
temperatures, and print out the following information
a) Print whether it is warmer inside, or outside or whether the temperatures are
the same.
b) If either temperature is below freezing then print “it is freezing outside/inside”
c) If either temperature is above 30 then print “it is hot outside/inside”
Loops
It is often useful to be able to repeat a block of code a pre-defined number of times.
This is one example of a loop. There are several ways to code a loop in Java – one
of which is the “for loop”.
The following code fragment will count from 1 to 5:
for(counter=1; counter<6; counter++)
{
System.out.println(counter);
}
The first line contains the reserved word for – followed by three statements in
brackets. The first of these (ie counter=1) is the starting condition. This means that
the first time the loop executes, the variable counter is set to 1. The second (ie
counter<6) is the continuing condition. This means that the loop will continue so
long as that condition is true (ie in this case, the counter variable is less than 6). The
final staement (ie counter++) is executed each time the loop is repeated (ie in this
case, the counter is incremented). The block of code following this line is then
executed in the loop. In this case the braces aren’t essential because there is only a
single statement – however, if there is more than one statement in the loop then they
are required.
NB the first line is not terminated with a semi-colon. This is because it is not, on it’s
own, a statement.
3) Write a program that counts from 1 to 5. Use the code fragment above as a basis
for this. When the program is working experiment with it. Try removing the braces
around the println stament. Change the println statement to a print statement, and
modify the code so that a space is inserted between each number printed.
4) The Fibonacci series is a sequence of numbers, each of which is the sum of the
two that precede (starting with 1,1). Write a program that prints a 50-number
Fibonacci series (in several columns for easy reading) - this will start as follows:
Fibonacci series
1 1 2 3 5
8 13 21 32 53
...
5) Write a program that will use loops to print out the following triangle of numbers:
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
6 6 6 6 6 6
6) Once you have done this, modify your program so that it is neatly formatted as
shown here:
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
6 6 6 6 6 6
HINT – remember that a space is a character – count the spaces!
7) A popular children's song goes like this:
1 man went to mow, went to mow a meadow,
1 man and his dog, went to mow a meadow.
2 men went to mow, went to mow a meadow,
2 men, 1 man and his dog, went to mow a meadow.
3 men went to mow, went to mow a meadow,
3 men, 2 men, 1 man and his dog, went to mow a meadow.
etc.
Write a program that will print this song. Store the number of men in a variable,
and make sure that your program can handle any number of verses. test it by
changing the number of men. Try printing out “100 men went to mow”!.
HINT - you will need nested loops for this (ie a loop inside a loop), and don't
forget your grammar (it is 2 men but 1 man)!!
Download