If Statements Flashcards
Asks the user to enter a number,
- Outputs the day that the number represents,
- Validates the number so that an error message displays if the number
is not a valid weekday number.
INPUT number IF number == 0 THEN DISPLAY “Monday” ELSE-IF number == 1 THEN DISPLAY “Tuesday” ELSE DISPLAY “INVALID NUMBER ENTERED”
Write an algorithm in pseudocode which:
- asks the user to input 2 numbers
- outputs the larger of the two numbers along with a suitable message
INPUT num1 INPUT num2 IF num1 > num2 THEN OUTPUT num1 + “is the largest number” ELSE OUTPUT num2 + “is the largest number” END-IF
A dog that is 5 years old is equivalent to a 42 year old human. You need to
write a program that converts the age of a dog to the equivalent age of a
human.
Write an algorithm in pseudocode which:
- Asks for the age of the dog in years
- If the age is 2 or less, the human equivalent is 12 times the age
- If the age is more than 2, the human equivalent is 24 for the first 2
years, plus 6 for every additional year.
INPUT dogs_age IF dogs_age <= 2 human_dog_years = dogs_age * 12 ELSE extra_years = dogs_age – 2 human_dog_years = 24 + extra_years
A taxi firm charges £3 for the first mile and £2 for every mile after that. If
there are 5 or more passengers, an extra 50% is added to the price.
Write an algorithm in pseudocode which calculates the cost of a journey.
The algorithm should:
- Ask the user to enter the number of passengers
- Ask the user to enter the distance (as an integer)
- Calculate the price of the journey
- Output the price on the screen
INPUT Distance INPUT Passengers Extra = Distance – 1 CostofExtra = Extra * 2 Cost = 3 + CostofExtra IF Passengers > 4 THEN Surcharge = Cost / 2 Cost = Cost + Surcharge END IF OUTPUT COST
In a factory, the wages earned by a worker is either £2 for every teddy
bear they have made or £5 for every hour they have worked, whichever is
higher.
Write an algorithm in pseudocode which:
- Allows the user to input the number of teddy bears made and the
number of hours worked
- Calculates the wages for the number of teddy bears made
- Calculates the wages for the number of hours worked
- Outputs the larger of the two results.
INPUT TeddyBears INPUT Hours PerTeddyBear = 2 * TeddyBears PerHour = 5 * Hours IF PerTeddyBear > PerHour THEN OUTPUT PerTeddyBear ELSE OUTPUT PerHour END IF
An isosceles triangle is one which has at least two equal length sides.
Write an algorithm in pseudocode which:
- Asks the user to enter the lengths of a triangle
- Works out if the triangle is isosceles
- Outputs a message stating whether the triangle is isosceles or not.
INPUT Length1, Length2, Length3 IF Length1 = Length2 THEN Output “Isosceles” ELSE IF Length1 = Length3 THEN Output “Isosceles” ELSE IF Length2 = Length 3 THEN Output “Isosceles” ELSE OUTPUT “Not Isosceles” END IF END IF END IF