LambdaLabTM
Computer Science · Class 11 · Sample Programs
Sample programsWith solutions⏱️ 10 min read

More Practice Programs

Five more, each in the same four-step shape: ask, cast, calculate, show. Read the problem, try to write it yourself, and only then look at the solution underneath — that is where the learning is.

11 · Celsius to Fahrenheit

Problem: ask for a temperature in Celsius and display it in Fahrenheit. The formula is F = (C × 9/5) + 32.

temperature.py
c = float(input('Temperature in Celsius: '))

f = c * 9 / 5 + 32

print(c, 'Celsius is', f, 'Fahrenheit')
Output
Temperature in Celsius: 37
37.0 Celsius is 98.6 Fahrenheit

No brackets are needed round c * 9 / 5 because * and / already run before +. Put them in anyway if it helps you read it — (c * 9 / 5) + 32 is the same program.

temperature.py

22 · Simple interest

Problem: ask for the principal, the rate of interest and the time in years. Print the simple interest and the total amount to be paid back. The formula is SI = (P × R × T) / 100.

interest.py
p = float(input('Principal amount: '))
r = float(input('Rate of interest per year: '))
t = float(input('Time in years: '))

si = (p * r * t) / 100
amount = p + si

print('Simple interest =', si)
print('Amount to be paid =', amount)
Output
Principal amount: 10000
Rate of interest per year: 7.5
Time in years: 3
Simple interest = 2250.0
Amount to be paid = 12250.0
Forgetting the / 100
Drop the / 100 and the program still runs, quietly making you a hundred times richer: 225000 instead of 2250. A logical error, with no message to warn you.
interest.py

33 · Area and perimeter of a rectangle

Problem: ask for the length and breadth of a rectangle. Print its area and its perimeter. Area = l × b, perimeter = 2 × (l + b).

rectangle.py
l = float(input('Length: '))
b = float(input('Breadth: '))

area = l * b
perimeter = 2 * (l + b)

print('Area =', area)
print('Perimeter =', perimeter)
Output
Length: 12
Breadth: 5
Area = 60.0
Perimeter = 34.0

The brackets in the perimeter are doing real work. Without them, 2 * l + b doubles only the length — 29 instead of 34, with no complaint from Python.

rectangle.py

44 · Swapping two values

Problem: ask for two numbers and print them with their values exchanged. This one is a classic because the obvious answer is wrong.

swap_broken.py
x = 10
y = 20

x = y      # x is now 20 — and the old 10 is gone forever
y = x      # so this copies 20 back into y

print('x =', x, 'and y =', y)
Output
x = 20 and y = 20

Both boxes end up holding 20. The moment x = y runs, the original 10 has nowhere left to live. The fix is a third box to hold it while you move things:

swap.py
x = float(input('First number: '))
y = float(input('Second number: '))

temp = x     # keep x safe
x = y
y = temp

print('After swapping: x =', x, 'and y =', y)
Output
First number: 10
Second number: 20
After swapping: x = 20.0 and y = 10.0
Python can do it in one line
x, y = y, x swaps them with no temp at all — Python works out the whole right-hand side first, then assigns. Learn the temp version anyway: it is the one exams ask for, and it is how every other language does it.
swap.py

55 · Seconds into hours, minutes and seconds

Problem: ask for a number of seconds and print it as hours, minutes and seconds. 3725 seconds should come out as 1 hour, 2 minutes and 5 seconds.

Same tool as the height program: // for how many whole units fit, % for what is left over.

seconds.py
total = int(input('Number of seconds: '))

hours = total // 3600          # 3600 seconds in an hour
remaining = total % 3600       # what is left after the hours

minutes = remaining // 60      # 60 seconds in a minute
seconds = remaining % 60       # and what is left after those

print(hours, 'hour(s),', minutes, 'minute(s),', seconds, 'second(s)')
Output
Number of seconds: 3725
1 hour(s), 2 minute(s), 5 second(s)
seconds.py

6Now write these on your own

No solutions for these. Each one is a rearrangement of something above, so if you can do the five programs on this page you can do all six of these:

Percentage of marks

Ask for marks in five subjects out of 100 each. Print the total and the percentage.

Volume of a cuboid

Ask for length, breadth and height. Print the volume and the total surface area.

Split a bill

Ask for the bill amount and the number of friends. Print what each one pays.

Kilometres to metres

Ask for a distance in km. Print it in metres and in centimetres.

Age in days

Ask for an age in years. Print roughly how many days, hours and minutes that is.

Bill with GST

Ask for the price and the GST percent. Print the tax and the final price.

Use the box below as your notebook — it is a full Python file, so write any of them here and press Run.

my_program.py

7Recap

Key Takeaway
Every program on this page is the same four steps — ask with input(), cast with int() or float(), calculate into named variables, show with a labelled print(). What changes is only the formula in the middle, and the brackets it needs.
Quick Check

Why does x = y followed by y = x fail to swap two values?

Quick Check

Which pair turns 3725 seconds into whole minutes and leftover seconds?