Chapter 7- Algorithm Workbench

NOTE: There are no TABS on the codes. THESE ARE MY PERSONAL solutions to the problem. Copy at your own risk.

1. Design an algorithm that prompts the user to enter a positive nonzero number and validates the input.

//This program asks the user to enter a positive nonzero number and validates the input.
Declare Integer value
//Get a value from the user
Display “Enter a positive nonzero number:”
Input value
//Validate input
While value <= 0 Display "ERROR: The value must be a positive nonzero number." Display "Enter a positive nonzero number:" Input value End While 2. Design an algorithm that prompts the user to enter a number in the range of 1 through 100 and validates the input. //This program asks the user to enter a number in the range of 1 through 100 and validates the input. Declare Integer value //Get a value from the user Display "Enter a number between 1 and 100." Input value //Validate While value < 1 OR value > 100
Display “ERROR: The number must be between 1 and 100.”
Display “Enter a number between 1 and 100.”
Input value
End While

3. Design an algorithm that prompts the user to enter “yes” or “no” and validates the input. (Use a case-insensitive comparison)

//This program asks the user to enter “yes” or “no” and validates the input.
Declare String response
//Get a response from the user
Display “Enter yes or no:”
Input response
//Validate
While toLower(response) != “yes” AND toLower(response) != “no”
Display “ERROR: You must enter yes or no.”
Display “Enter yes or no:”
Input response
End While

4. Design an algorithm that prompts the user to enter a number that is greater than 99 and validates the input.

//This program asks the user to enter a number that is greater than 99 and validates the input.
Declare Integer value
//Get a value from the user
Display “Enter a number that is greater than 99:”
Input value
//Validate input
While value <= 99 Display "ERROR: The value must be greater than 99." Display "Enter a number that is greater than 99:" Input value End While 5. Design an algorithm that prompts the user to enter a secret word. The secret word should be at least 8 characters long. Validate the input. //This program asks the user to enter a secret word at least 8 characters long and //validates the input. Declare String response //Get a response from the user Display "Enter a secret word at least 8 characters long:" Input response //Validate While length(response) < 8 Display "ERROR: secret word must be at least 8 characters long." Display "Enter a secret word at least 8 characters long:" Input response End While

Leave a Reply

Your email address will not be published. Required fields are marked *