Algorithm
Algorithm
CPEN 21A
BSCE 2-1 September 10,
2019
1.) Ask the user to enter a length in centimeters. If the user enters a negative length, the
process should tell the user that the entry is invalid. Otherwise, the process should convert
the length to inches and print out the result. There are 2.54 centimeters in an inch.
STEP 1: START
STEP 2: GET the value of length in centimeter
STEP 3: IF length >= 0 THEN
CALCULATE inch = length / 2.54
DISPLAY inch
ELSE IF length is < 0 THEN
DISPLAY entry is invalid
END IF
STEP 4: STOP
2.) Ask the user for a temperature. Then ask what units, Celsius or Fahrenheit, the
temperature is in. Your process should convert the temperature to the other unit. The
9 5
conversions are F= (C) + 32 and C = (F-32).
5 9
STEP 1: START
STEP 2: GET the value of variable temperature, Celsius = C or Fahrenheit = F
STEP 3: IF temperature = C THEN
9
CALCULATE F = (C)
5
DISPLAY F
ELSE IF temperature = F THEN
5
CALCULATE C = 9(F-32)
DISPLAY C
END IF
STEP 4: STOP
3.) Ask the user to enter a temperature in Celsius. The process should print a message
based on the temperature:
If the temperature is less than -273.15, print that the temperature is below absolute
zero.
If the temperature is exactly -273.15, print that the temperature is absolute 0.
If the temperature is between -273.15 and 0, print that the temperature is below
freezing.
If it is 0, print that the temperature is at the freezing point.
If it is between 0 and 100, print that the temperature is in the normal range.
If it is 100, print that the temperature is at the boiling point.
If it is above 100, print that the temperature is above the boiling point.
STEP 1: START
STEP 2: GET the value of variable temperature in Celsius
STEP 3: IF temperature < -273.15 THEN
DISPLAY temperature is invalid because it is below absolute zero
ELSE IF temperature == 273.15 THEN
DISPLAY temperature is absolute zero
ELSE IF temperature is > 273.15 and < 0 THEN
DISPLAY temperature is below freezing
ELSE IF temperature == 0 THEN
DISPLAY temperature is at freezing point
ELSE IF temperature > 0 and < 100 THEN
DISPLAY temperature is in the normal range
ELSE IF temperature == 100 THEN
DISPLAY temperature is at the boiling point
ELSE IF temperature > 100 THEN
DISPLAY temperature is above the boiling point
END IF
STEP 4: STOP
4.) Ask the user how many credits they have taken. If they have taken 23 or less, print that
the student is a freshman. If they have taken between 24 and 53, print that they are a
sophomore. The range for juniors is 54 to 83, and for seniors it is 83 and over.
STEP 1: START
STEP 2: GET the value of variable credits
STEP 3: IF credits <= 23 THEN
DISPLAY freshman
ELSE IF credits >= 24 and <=53 THEN
DISPLAY sophomore
ELSE IF credit >= 54 and <= 83 THEN
DISPLAY junior
ELSE IF credit >= 84 THEN
DISPLAY senior
END IF
STEP 4: STOP
5.) Ask the user how many items they are buying and prints the total cost. A store charges
$12 per item if you buy less than 10 items. If you buy between 10 and 99 items, the cost is
$10 per item. If you buy 100 or more items, the cost is $7 per item.
STEP 1: START
STEP 2: GET the value of variable item
STEP 3: IF item < 10 THEN
CALCULATE total cost = item*12
DISPLAY total cost
ELSE IF item >= 10 and <= 99 THEN
CALCULATE total cost = item*10
DISPLAY total cost
ELSE IF item >= 100 THEN
CALCUALTE total cost = item*7
DISPLAY total cost
END IF
STEP 4: STOP
6.) Ask the user for an hour between 1 and 12, ask them to enter am or pm, and ask them
how many hours into future they want to go. Print out what the hour will be that many hours
into future, printing am or pm as appropriate.
STEP 1: START
STEP 2: GET the value of variable time, am = a or pm = p
STEP 3: GET the value of variable hour
STEP 4: CALCULATE future time = hour + time
STEP 5: WHILE future time > 12
Future time = future time – 12
c=a
a=b
b=c
ENDWHILE
STEP 4: DISPLAY future time
STEP 5: STOP
7.) Let the user play Rock-Paper-Scissors against the computer. There should be five rounds,
and after those five rounds, your program should print out who won and lost or that there is a
tie.
STEP 1: START
STEP 2:
8.) Ask the user for a weight and converts it from kilograms to pounds. Whenever the user
enters a weight below 0, the process should tell them that their entry is invalid and the ask
them again to enter a weight.
STEP 1: START
STEP 2: GET the value of variable weight in kilograms
STEP 3: WHILE weight < 0
DISPLAY invalid weight, try again
GET another value of variable weight in kilograms
ENDWHILE
STEP 4: CALCULATE pounds = weight*2.20462
STEP 5: DISPLAY pounds
STEP 6: STOP
9.) Ask the user to enter a password. If the user enters the right password, the process should
tell them they are logged in to the system. Otherwise, the process should ask them to reenter
the password. The user should only get five tries to enter the password, after which point the
process should tell them that they are kicked off the system.
STEP 1: START
STEP 2: SET password == qwertyuiop
STEP 3: SET tries = 0
STEP 4: WHILE tries < 5
GET the password
IF password == qwertyuiop THEN
DISPLAY successfully logged in
ELSE password != qwertyuiop
DISPLAY incorrect password, try again
Tries +=1
END IF
ENDWHILE
STEP 5: IF tries == 5 THEN
DISPLAY you cannot log in anymore
END IF
STEP 6: STOP
10.) Allow the user to enter any number of test scores. The user indicates they are done by
entering in a negative number. Print how many of the scores are A’s (90 or above). Also print
out the average.
STEP 1: START
STEP 2: GET the value of variable test score
STEP 3: WHILE test score >= 90
DISPLAY test score
A += 1
Divider =+ 1
CALCULATE total score = total score + test score
ENDWHILE
STEP 4: CALCULATE average score = total score/counter
STEP 5: DISPLAY A
STEP 6: DISPLAY average
STEP 7: STOP
11.) Ask the user for two numbers and computes their GCD. The GCD (greatest common
divisor) of two numbers is the largest number that both are divisible by. For instance, GCD(18,
42) is 6 because the largest number that both 18 and 42 are divisible by 6. Shown below is a
way to compute the GCD, called Euclid’s Algorithm.
First compute the remainder of dividing the larger number by the smaller
number
Next, replace the larger number with the smaller number and the smaller
number with the remainder.
Repeat this process until the smaller number is 0.
The GCD is the last value of the larger number.
14.) Use a for loop to print the numbers 8, 11, 14, 17, 20, . . . 83, 86, 89.
STEP 1: START
STEP 2: FOR (SET i = 8; i < 90, SET i = i+3) DO
DISPLAY i
END FOR
STEP 3: STOP
15.) Use a for loop to print the numbers 100, 98, 96, . . . , 4, 2.
STEP 1: START
STEP 2: FOR (SET i + 100; i < 0, SET i = i-2) DO
DISPLAY i
END FOR
STEP 3: STOP