See how far you can get with programming a solution to the following problems. If you get stuck, you can highlight the example given below each question and copy and paste it into the compiler.
Write a program that takes a width and height as input, and draws a rectangle made up of the character *
import system;
import maths;
void main()
{
int width = readInt();
int height = readInt();
int w = 0;
int h = 0;
while(h < height)
{
while(w < width )
{
write("*");
w = w + 1;
}
w = 0;
h = h + 1;
writeLine();
}
}
Write a program that uses the maths import to generate a random number between 1 and 100, then asks you to guess the number. The program tells you if its number is higher or lower than the number you gave and the program ends when you guess correctly.
import system;
import maths;
void main()
{
int n = random(1, 101);
int guess = 0;
writeLine("Guess my number from 1 to 100.");
while( ! (guess == n) )
{
guess = readInt();
if( n > guess ) writeLine("No, my number is greater");
if( n < guess ) writeLine("No, my number is smaller");
}
writeLine("Well done!");
}
Write a program that prints out the Fibonacci sequence (where the next number is the sum of the previous two numbers, i.e. 1, 1, 2, 3, 5, 8, 13, 21)
import system;
import maths;
void main()
{
int a = 1;
int b = 1;
while(true)
{
int result = a + b;
writeLine( result );
a = b;
b = result;
}
}Write a program that writes out the lyrics for the song 99 bottles of beer, starting at any given number of bottles.
import system;
import maths;
void main()
{
int bottles = readInt();
while(bottles > 0)
{
write(bottles);
writeLine(" bottles of beer on the wall");
write(bottles);
writeLine(" bottles of beer!");
writeLine("Take one down, pass it around");
bottles = bottles - 1;
write(bottles);
writeLine(" bottles of beer on the wall!");
writeLine();
}
}