110 likes | 244 Vues
This guide presents algorithms for handling user input to determine the larger of two or three numbers and to compute distance traveled with average miles per gallon based on user-provided data. The first algorithm prompts the user for two numbers and prints the larger one. The second algorithm calculates the distance traveled by considering the gallons used and the odometer readings to compute average MPG. Examples illustrate expected outputs when specific inputs are given, enhancing understanding of conditional statements and calculations in programming.
E N D
Selection Structure by Yonglei Tao
Conditional Statements • Write an algorithm that asks the user for two numbers, and print the larger one. Get the values of a, b from user If the value of a is greater than the value of b then print “The larger number is ” & a Else print “The larger number is ” & b • Test 1:What does the program print if the user enter 10, 3? • Test 2:What does the program print if the user enter 2, 11?
Example • Write an algorithm to compute the distance traveled and the average miles per gallon on a trip when given as input the number of gallons used and the starting and ending mileage readings on the odometer. get gallons, start, end distance = end – start mpg = distance / gallons print mpg if mpg > 25.0 then print “You are getting good gas mileage”
Exercise • Write an algorithm that asks the user to enter three numbers, and computes and prints out the largest number