Challenges

Test your skills!!

Challenges

Write a program that will allow a user to enter their name (string), their age (integer) and their favourite TV program (string). The program will then display the information entered and some additional text on separate lines. An example of the input and output from the program is shown below.

Input

Please enter your name?
Lister
What is your age?
39
What is your favourite TV program?
Red Dwarf

Output

Lister
is
39
years old and likes
Red Dwarf

1. Three In, Three Out

Write a program that will ask the user to type in their first name and surname. The program will then display the two names in reverse order.

Input

David
Tennant

Output

Tennant David

2. Name Swapper

Now edit program 1 so that the information entered is displayed differently as shown in the output box below. Note - your output will now have to display variables and text together.

Input

Please enter your name?
Lister
What is your age?
39
What is your favourite TV program?
Red Dwarf

Output

Lister 39
Likes watching Red Dwarf

3. Three In, Three Out (formatted)

Ask your user to enter the four separate sections of a post code. Postcodes take the following form: letters, number, number, letters
Once entered the postcode should be displayed with a space in the middle.

Input

SP
8
1
HL

Output

SP8 1HL

4. Postcode Formatter

Ask your user to enter the length and width of a rectangle. Your program should calculate the area of the rectangle (length*width) and display the result with a suitable message.

Input

Please enter the following values in cm.
Please enter the length of the rectangle.
12
Please enter the width of the rectangle.
6

Output

The area of the rectangle is:
72 square centimetres

5. Calculate the Area of a Rectangle

Ask your user to enter the radius of a circle. Your program should use what they have entered to
calculate the area of the circle (3.14*radius*radius) and display the result.

Input

Please enter the following values in cm.
Please enter the radius of the circle.
16

Output

The area of the circle is:
803.84 square centimetres

6. Calculate the Area of a Circle

Write a program that inputs two individual integers between 0 and 9. The program should then
perform a calculation and store a single number in a third variable called ‘total’. As shown below, the
total should then be displayed on the screen.

Input

2
6

Output

26

7. Number Generator (2 digits)

Adapt program 7 to work for 3 numbers instead of 2.

Input

3
5
7

Output

357

8. Number Generator (3 digits)

A hydrocarbon is a molecule made up of linked Carbon (C) atoms with Hydrogen (H) atoms branching off each Carbon. Your program will ask the user to enter the number of Carbon atoms in a hydrocarbon and use the number entered to then calculate the number of Hydrogen atoms using the formula below. Both numbers should be stored.

number of H atoms = (number of C atoms x 2) + 2

The atomic weight of the molecule is calculated by multiplying the number of carbon atoms by 12 and adding the number of hydrogen atoms. The number of C and H atoms along with the atomic weight should be displayed as shown in the output below.

Input

Enter the number of carbon atoms?
3

Output

The atomic mass of C3H8 is 44

9. Calculating the Atomic Weight of Hydrocarbons (Alkanes)

The “standard scratch” of a golf course is calculated by adding together the number of shots it should take to complete each hole. This score is then adjusted depending on the difficulty of the course.
For example:

2 holes take 5 shots (par 5) 2x5 = 10
10 holes take 4 shots (par 4) 10x4 = 40
6 holes take 3 shots (par 3) 6x3 = 18 10+40+18 = 68 shots in total
Difficulty adjustment -2 68-2 = 66
Standard Scratch = 66


Write a program that allows the user to enter the information required to calculate the standard scratch
score of a golf course. The standard scratch should then be calculated and displayed.

Input

How many par 3 holes are there?
6
How many par 4 holes are there?
10
How many par 5 holes are there?
2
What is the difficulty adjustment for the course?
-2

Output

The Standard Scratch for the course is:
66

10. Standard Scratch

A program is required to store a users address in a single string. The user should be asked three questions:

What is your house number?
What is the name of your street?
What town do you live in?


The program will then combine the users answers in a single string with each answer being separated by a comma and store the result. Finally the program will display the result on the screen.

Input

What is your house number?
13
What is the name of your street?
Aberlove Drive
What town do you live in?
Buckhaven

Output

13, Aberlove Drive, Buckhaven

11. Formatting an Address

UK telephone numbers are often displayed in the following format:

area code, space, local code

All UK telephone numbers start with a ‘0’.
Write a program that will allow a user to enter the area code and local code of a telephone number.
The program will then display the two codes, separated by a space.

Input

Please enter an area code:
01592
Please enter the local code:
583403

Output

You have entered:
01592 583403

12. Formatting Telephone Numbers

Write a program that asks the user if they would like some advice. If they enter Y, provide them with an amusing message.

Input

Would you like some advice?
Y

Output

Always know where your towel is.

13. Advice Please

During a game of darts, the highest score that can be achieved in a single turn is 180. The lowest is 0.
Write a program that will allow a dart player to enter their score. The program should congratulate the player if their score was over 100. If the player scores less than 10 they should be told that some practice is required.

Input

Please enter your score:
125

Input

Please enter your score:
7

Output

What a great score! Well done.

Output

That was rubbish. Get practicing!

14. Darts

Three friends have been collecting money for charity. A local company has offered to double the amount of money they collect if they raise over £1000. Write a program that allows the friends to enter there individual amounts. The program should then add the three amounts and store the total. If the total is greater or equal to 1000 the total should be doubled. Finally the total should be displayed.

Input

Please enter the first amount raised. Input Output
398
Please enter the second amount raised.
193
Please enter the third amount raised.
478

Output

A total of £1069 was raised.
This will be doubled to £2138.

15. Charity Collection

Program 5 asked you to calculate the area of a rectangle. Expand this program so that it calculates the area of two rectangles. Once both areas have been calculated your program should decide which rectangle has the larger area and display a suitable message.

Input

Rectangle 1
Please enter the length:
12
Please enter the width:
6
Rectangle 2
Please enter the length:
7
Please enter the width:
9

Output

Rectangle 1 has the largest area

16. Calculate the Area of a Rectangle (Part 2)

Expand program 13 to include an alternative message if the user types N. An error message should be given to the user if they enter anything other than Y or N.

Input

Would you like some advice?
D

Output

Sorry, you were asked to enter Y or N.

17. Advice Please (Part 2)

As a car’s tyres and brakes wear out it takes longer for a car to stop. A list of recommended stopping distances are shown below.

20 miles per hour 6 metres to stop
30mph 14m
40mph 24m
50mph 38m
60mph 55m
70mph 75m


Write a program to analyse a braking distance test. The user should be asked to enter the speed (mph) they were doing and the distance (m) it took them to stop. If the distance is longer than the recommended stopping distance the user should be advised to go for a tyre and brake check.

Input

Please enter the test speed (mph).
40
Please enter the tested stopping distance (m).
26.7

Output

Your car failed the braking distance test.
Submit your car for a tyre & brake test soon.

18. Tyre & Brake Wear

At normal atmospheric pressure, water changes state to a solid at 0oC or below and a gas at 100oC or above. It remains a liquid at any other temperature. Write a program that will return “solid”, “liquid” or “gas” to the user depending on the temperature they enter.

Input

Enter the current temperature.
78

Output

At 78 degrees centigrade, water will be a liquid.

19. Solid, Liquid, Gas

The local company have decided that due to current financial pressure than can not afford to double any amount of money raised over £1000. The following new decisions are made:

a) any amount raised less than £1000 has a £100 bonus (for example £345 raised = £445 total)
b) the company will still double the amount raised between £1000 and £2000 (for example £1282 raised = £2564 total)
c) if the amount is over £2000 the initial £2000 is doubled but any amount after that is not (for example £2054 raised = 2*£2000 + £54 = £4054 total)


Rewrite program 15 to take account of the above decisions.

Input

Please enter the first amount raised.
745
Please enter the second amount raised.
690
Please enter the third amount raised.
1024

Output

A total of £2459 was raised.
With the company bonus, this is £4459.

20. Charity Collection (Part 2)

A naughty pupil has been given lines to copy as a punishment from their Computing teacher. The have been asked to type out “I must not behave like muppet in class” 20 times. Write a program that asks a pupil to enter a sentence. The same sentence should then be displayed 20 times.

Input

Which sentence would you like copied?
I must not behave like a muppet in class.

Output

I must not behave like a muppet in class.
I must not behave like a muppet in class.
I must not behave like a muppet in class.
I must not behave like a muppet in class.
I must not behave like a muppet in class.
x20

21. Lines Cheat

Adapt Program 20 so that the total is displayed 3 times to emphasise the amount of money raised.

Input

Please enter the first amount raised.
398
Please enter the second amount raised.
193
Please enter the first amount raised.
478

Output

A total of £1069 was raised.
This will be doubled to:
£2138!!!
£2138!!!
£2138!!!

22. Charity Collection (Part 3)

An artist has been experimenting with making art from peoples name. Their first attempt involves repeatedly swapping someone’s firstname and surname to create a pattern. Write a program that asks for two names to be entered and then uses the input to display the correct pattern.

Input

Please enter first name:
Bob
Please enter surname:
Tomlinson

Output

Bob Tomlinson Bob Tomlinson
Tomlinson Bob Tomlinson Bob
Bob Tomlinson Bob Tomlinson
Tomlinson Bob Tomlinson Bob
Bob Tomlinson Bob Tomlinson
Tomlinson Bob Tomlinson Bob

23. Name Switch

In cricket a bowler bowls 6 balls at a time. This is called an ‘over’.
Write a program that allows 6 scores to be entered (for example, 0,2,0,0,4,6). One for each ball in the over. The total scored in that over should then be displayed.

Input

Please enter the score for each ball. I
0
2
0
0
4
6

Output

This over’s score was: 12

24. Cricket Over

A weather experiment is set up to calculate the average temperature on a mountain peak during the course of a week. The following measurements are taken at 1pm every day.

Mon 12oC Tue 14oC Wed 7oC
Thur 6oC Fri 7oC Sat 11oC
Sun 11oC


A program is required to allow the experimenter to enter the 7 temperatures. The average for the week should then be displayed, to two decimal places, as shown below.

Input

Please enter the seven temperatures.
12
14
7671
1
11

Output

This week’s average was:
9.71 degrees centigrade

25. Average Temperature

The naughty pupil forgot to hand their lines in and now has more to do. Adapt program 21 to allow the
pupil to select how many lines the program produces.

Input

Which sentence would you like copied?
I must not behave like a muppet in class.
How many times would you like this copied?
54

Output

I must not behave like a muppet in class.
I must not behave like a muppet in class.
I must not behave like a muppet in class.
I must not behave like a muppet in class.
I must not behave like a muppet in class.
x54

26. Lines Cheat (Part 2)

The following year the three friends recruit many more charity raisers. Adapt program 22 to ask for the number of people raising money to be entered. The program will then total up the money raised by the group and display the output as before.

Input

How many charity raisers were there?
6
Enter the total raised by each:
238
624
546
333
651
174

Output

A total of £2566 was raised.
This will be increased to:
£4566!!!
£4566!!!
£4566!!!

27. Charity Collection (Part 4)

Jack and Jill are competing in the annual Pailwater darts competition. Each competitor throws 9 darts (3 lots of 3 darts) each. The player with the highest total score is the winner. Write a program to calculate both scores and announce the winner.

Input

Please enter the scores for player one
34
60
100
Please enter the scores for player two
45
36
24

Output

Player one scored: 194
Player two scored: 105
Player one wins!!

28. Pailwater Darts Tournament

Naebor High School have organised a fitness challenge with nearby Wisnaeme Academy. Each school picks their three fittest pupils and record the average time it takes them to do 50 press ups. Write a program that asks a user to enter a time for pupil as shown below (note that the pupils are numbered).
The program should then display the average time (to 2 decimal places).

Input

Enter the time in seconds for each pupil.
Pupil 1
75
Pupil 2
56
Pupil 3
60

Output

The average time was:
63.67 seconds

29. Press Up Challenge

A math teacher wishes to show her class a variety of number patterns. She starts by showing her class a list of odd numbers, explaining that the pattern starts at 1 and then misses out every second number:
1 3 5 7 9 11
Write a program to display the list of odd numbers shown above.

Input

-

Output

Odd Numbers
1
3
5
7
9
11

30. Number Patterns

Adapt program 26 so that the user can choose the first and last odd numbers displayed.

Input

The following program will display odd numbers.
Enter the first number in the list
5
Enter the last number in the list
13

Output

Odd Numbers List
5791
1
13

31. Number Patterns (Part 2)

The program should give the user an error message if they enter the wrong password. A message “Entry gained!” should be displayed when the password in entered correctly. You may choose the
password.

Input

Please enter the password.
snool
Sorry, Incorrect.! Try again.
Please enter the password.
ornery

Output

Entry gained!

32. Password

You have decided that your user definitely requires some advice. Edit program 17 so that will continue to ask if your user wants advice until they enter Y. Include an amusing message if they enter N and an error message if they don’t enter Y or N.

Input

Would you like some advice?
H
Please enter Y or N only.
Would you like some advice?
N
Don’t be silly. You definitely need advice!
Would you like some advice?
Y

Output

Don’t feed the Trolls!

33. Advice Please (Part 3)

Edit program 18 to ensure that the speed that is entered for the brake distance test is always greater than or equal to 20mph and the stopping distance is always greater than 0.

Input

Please enter the test speed (mph).
15
Sorry, must be 20 or more. Enter again.
Please enter the test speed (mph).
40
Please enter the tested stopping distance (m).
-5.6
Sorry, please enter a value greater than 0.
Please enter the tested stopping distance (m).
23.2

Output

Your car passed the braking distance test.
Well done

34. Tyre & Brake Wear (Part 2)

You have £200 to spend on your birthday. Write a program that will ask you to enter the price of each present you want until your total is over £200. The program should produce the output shown.

Input

Please enter the price of each present:
35
100
50
45

Output

Limit Exceeded.
You can’t include the £45 present.

35. No More Presents

The minimum and maximum scores that can be achieved with three darts are 0 and 180.
Edit program 28 to validate your input (make sure the scores entered are between 0 and 180). Ensure you give your user an appropriate error message if they type in a wrong value.

Input

Please enter the scores for player one
34
204
Invalid Score! Enter again please:
60
100
Please enter the scores for player two
45
36
24

Output

Player one scored: 194
Player two scored: 105
Player one wins!!

36. Pailwater Darts Tournament (Part 2)

Adapt program 25 to validate each temperature. Inputs should be between -40oC and 55oC.
As before, the program should output the average temperature for the week but this time the output should be displayed as an integer.

Input

Please enter the seven temperatures.
12
14
7
95
Please enter a temperature between -40 and 55.
671
1
11

Output

This week’s average,
rounded to 0 decimal places was:
10 degrees centigrade

37. Average Temperature (Part 2)

A game is created where a user is required to guess an unknown number between 1 and 100. Each time the user guesses the program informs them if their guess in too high, too low or correct. The guessing game only finishes when the user’s guess matches the unknown number. An example of the program running (input and output) is shown below.

Input & Output

Guess the hidden number between 1 and 100.
Enter your guess.
45
Your guess is too low. Try again.
86
Your guess is too high. Try again.
67
Your guess is too high. Try again.
50
Your guess is too low. Try again.
54
Your guess is too high. Try again.
52
Correct! Well Done.

38. Guess the Number

A program is required to display a list of 5 items for sale on a monitor. Store a list of items in an array and then display these items as output.

Input

-

Output

Mountain Bike
Ski Jacket
Electric Guitar
PS3 - 500Gb
Badminton Racquet

39. For Sale

Edit program 39 above to store a second list of 5 prices, one for each of the sale items. Display both the name and price of each sale item on the same line.

Input

-

Output

Mountain Bike - £200
Ski Jacket - £67
Electric Guitar - £330
PS3 - 500Gb - £120
Badminton Racquet - £15

40. For Sale (Part 2)

Edit program 24 to store the score for each of the six balls in the over in an array. Use the array to display the 6 scores, as well as the total, in the output.

Input

Please enter the score for each ball.
0
2
0
0
4
6

Output

This over’s score was: 12
With each ball scoring:
0
2
0
0
4
6

41. Cricket Over (Part 2)

You have been asked to write a program to store the names and ages of 4 competitors in a dance competition. The program should display the name of the competitor and which level of competition they should be entered in. ‘Junior’ competitors are less than 12 years old, ‘senior’ competitors are at least 18 years old. ‘Teen’ competitors are aged 12-17.

Input

Please enter a name:
Jean Gray
Please enter Jean Gray’s age:
13
Please enter a name:
Max Eisenhardt
Please enter Max Eisenhardt’s age:
24
Please enter a name:
James Howlett
Please enter James Howlett’s age:
15
Please enter a name:
Anna Marie
Please enter Anna Marie’s age:
9

Output

Names and Competition List:
Jean Gray - Teen
Max Eisenhardt - Senior
James Howlett - Teen
Anna Marie - Junior

42. Dance Group

A computer program stores the names, ages and height (the height of horses is measured in ‘hands’ - for example, 16) of fifteen horses in a riding school. The user of the program will be asked to select a horse by entering a maximum age and height of the horse they wish to ride. The data for the fifteen horses will be used to provide the user with a list of suitable. A horse is suitable if its age and height are both less than or equal to the values entered by the user.

Input

Please enter details of your horse.
Maximum height:
16
Maximum age:
15

Output

Black Beauty, 14 years, 15 hands
Francine, 8 years, 14 hands
Langrish, 15 years, 14 hands

43. Horse Hands

The size of a house is calculated by adding together the floor area of each room. Write a program that asks the user how many rooms are in a house, stores the length and width of each room (as entered by the user) and finally calculates the total floor area of a house. The output should be formatted as shown below.

Input

House floor area calculator.
Please enter the number of rooms:
3
Please enter the length (m) of room 1
3.56
Please enter the width (m) of room 1
4.7
Please enter the length (m) of room 2
2.26
Please enter the width (m) of room 2
3.69
Please enter the length (m) of room 3
4.5
Please enter the width (m) of room 3
4.7

Output

The total area is calculated as:
Room 1
3.56 x 4.7 = 16.732 metres squared
Room 2
2.26 x 3.69 = 8.3394 metres squared
Room 3
4.5 x 4.7 = 21.15 metres squared
Total Area = 46.22 metres squared

44. House Size

Katy is having a birthday party. She sends invites to 10 of her friends (Mellisa, Evelyn, Emmy, Karen, Margaret, Norma, Agnes, Billy, Robert & Arthur). Katy requires a computer program to store her friends names and whether or not they will be attending the party. Write a program that asks the user if each guest is attending. The program should store true or false for each person. The output from the program should be a list of names of everyone who is going to the party.

Input

Who’s going?
Mellisa?
Y
Evelyn?
Y
Emmy?
N
Karen?
N
Margaret?
Y
x10 guests

Output

Party List:
Mellisa
Evelyn
Margaret
Norma
Billy
Arthur

45. Who’s Going

Using the random function, adapt program 38 to make the program generate the number (from 1 to 100) that the user is asked to guess. The program should run as before, so the output should not change.

46. Guess the Number (Part 2)

A program generates two random integers between 1 and 1000. A user is asked to guess which of the two numbers is the largest. If they are correct, they score a point. The program repeats this process 10 times and then displays the players final score.

Input

I have generated two random numbers.
Which is the largest, 1 or 2?
Enter your choice.
1
x10 pairs of numbers and guesses

Output

Number 1 = 459
Number 2 = 321
Correct, number 1 was the largest.
x10 pairs of numbers and
results
Your final score was 6 correct.

47. Which is Bigger?

Good programs should allow for a variety of different inputs. For example, if the user of Advice Please (program 33) enters ‘y’ rather than ‘Y’ the program will not give them advice. Using the upper case function, convert the users input into upper case letters to ensure they are given advice, even if they enter ‘y’.

Input

Would you like some advice?
y

Output

Always run away from a Dragon.

48. Advice Please (Part 4)

Edit program 4 to ensure that the postcode is always displayed in capital letters. Even if the user inputs the letters in lower case.

Input

ky
8
1
hl

Output

KY8 1HL

49. Postcode Formatter (Part 2)

UK Postcodes are a maximum of 8 characters long (7 letters and numbers + 1 space). Make changes to program 49 to ensure that the user is given an error message if they enter a postcode longer than 8 characters.

Input

kyv
82
15
hlb

Output

KYV82 15HLB
Sorry - Postcode is longer than 8 characters

50. Postcode Formatter (Part 3)

A painter and decorator requires a small program that will calculate the number of pots of paint that will be required to complete a job The program should also include how many metres squared could be painted with the left over paint.

Input

Enter the area in m2 to be painted.
178
Enter the area (m2) that a single pot covers.
25

Output

You will need 10 points of paint.
You can paint 22 m2 with the left over paint.

51. Left Over Paint

A teacher wishes to demonstrate the relationship between ASCII codes and letters of the alphabet.
Write a program that will generate a random number between 97 and 122 (the ASCII values for lower case a to z). The program should display the generated number and ask the user to guess what letter of the alphabet the number represents. The user should be prompted, as shown below, if they get the answer wrong and be asked to enter another guess.

Input & Output

Guess which letter of the alphabet is
represented in ASCII by the number - 113
f
Sorry f would be 102
r
Sorry r would be 114
q
Correct, q = 113

52. The ASCII Game

The guess the number game currently allows the user to enter a real number as their guess. Adapt program 46 to include input validation, ensuring that the user can only enter an integer as their guess.

Input & Output

Guess the hidden number between 1 and 100.
Enter your guess.
45
Your guess is too low. Try again.
86.7
Not a whole number. Please enter again
86
Your guess is too high. Try again.
67
Correct! Well Done.

53. Guess the Number (Part 3)

Create a program that will generate 7 random scrabble letters, displaying the score of each letter.

Input & Output

Your letters are:
F - 4
Z - 10
E - 1
S - 1
U - 2
C - 4
H - 4

54. Scrabble Letters
If you need to get in touch with Mr McG then this is the way….

What's your name?
image/svg+xml
Thank you [name], that's great.

Can I ask where you are from?
image/svg+xml
Hey [name], can I ask where you are from?
image/svg+xml
Hey [name], which class are you in?
image/svg+xml
OK [name], thanks for that.

I will need your email address so I can get back to you!!
image/svg+xml
Thanks [name], nearly there.

What can I help you with?
image/svg+xml

Thanks [name] from [whichclass][location], Mr McG will get your message…"
[message]"…and will get back to you ASAP via [email]. Catch you soon.

image/svg+xml
Thank you [name], Mr McG got your message.
Thanks [name], Mr McG will get back to you as soon as he can!!

Have a great day!!

K McGuinness - 2018

No personal data will be shared with any third party. The only data processed via this website is anonymous browsing data to best manage your user experience.