Challenges I
Variable assignment 1
Use =
to assign the value 123 + 456
to a variable named x
.
Then print x
.
Multi-line hello world
Make your program output "hello" on one line, and "world" on the next line.
In other words, your program must output:
hello
world
Variable assignment 2
Use =
to assign the string "some string"
to a variable named something that
you choose. (But don't use x
.) Then print the variable.
Exploration
What happens if you multiply a string with a number? What do you think would make sense?
Find out by multiplying "test"
by 3 using the *
sign and printing the
result.
Two assignments
Use =
to assign the string "lesson"
to a variable named something that
you choose.
Then use =
to assign the number 5
to another variable.
Then print the first variable.
Then print the second variable.
Area of a rectangle
Write a program that stores 45
in a variable named width
and that stores
81
in a variable named height
.
Then multiply the two variables, storing the result in a variable named `area'.
Finally, print the calculated area
.
Adding to variables
Store the value 999
in a variable named number
.
Then, on a new line, store number + 1
into the number
variable, like this:
number = number + 1
This has the result of adding 1
to the number
variable.
Then print number
.
Imperial to metric
Store the number 6
in a variable named feet
.
Then store the number 3
in a variable named inches
.
Then store 30*feet + 3*inches
in a variable named cm
.
Finally, print cm
to see how many centimeters 6 feet and 3 inches are.
Years to hours
Bob just turned 35 years old.
Calculate and print how many hours Bob has lived.
You should assume that there are always 365 days in a year, and 24 hours in a day. (So ignore leap years.)
You can use variables to store the results of your calculations, or you can do the whole thing in one line.