Question: Practice 1 Write a stern program that asks the user to enter a number within some range. If the user tries to joke around and
Practice
Write a stern program that asks the user to enter a number within some range. If the user tries to joke around and input something incorrect, ask again, and again, and again, until they learn to get it right. Use input validation in a loop until the user enters correct input.
Example Output
Please enter a number :
Please enter a number :
Please enter a number :
Please enter a number : ldkfjdlj
Please enter a number : true
Please enter a number :
Please enter a number :
You entered: finally
Practice
Revisit any program in a previous lab and update it to include input validation. We recommend completely rewriting the program from scratch, instead of simply copying it and inserting input validation. This speeds up the process of learning a new programming language.
Example Output
Quotes on Demand!
Mark Twain
Jiddu Krishnamurti
Graham Priest
Choose wisely: klsdfj
Choose wisely:
Choose wisely:
Choose wisely: Mark Twain
Choose wisely:
Choose wisely:
"Scientific advances are often triggered by taking oddities seriously."
Graham Priest
Practice
Use a for loop to print the multiples of between and Do this by looping times and using a continue statement to skip loop iterations which are not multiples of
Example Output
Multiples of :
Practice
Use a for loop to print the multiples of between and Do this by looping only times, perhaps by changing the update expression of the loop.
Example Output
Multiples of :
Practice
Rewrite the program for Gentle Jerry's Generic Goods to be even better! This time add input validation, a loop to allow the user to buy more goodies, and an option to quit buying.
Example Output
Welcome to Gentle Jerry's Generic Goods!
Sword gold
Shield gold
Potion goldounce
Nothing, quit.
What would you like to buy? Sword
What would you like to buy?
How many?
What would you like to buy? Kdjfld
What would you like to buy?
What would you like to buy?
How much?
What would you like to buy? Your prices are getting damn expensive, Jerry!
What would you like to buy? I HATE YOU!!!!
What would you like to buy?
Swords gold
Potion gold
Subtotal gold
sales tax gold
Total gold, silver
Practice
Create a Guess the Number! game. Initialize an unsigned integer to store a random number in the range and declare another unsigned integer to store the user's guess. Ask the user for their guess. Then write a loop to keep running until the user guesses the number. If the user guesses the correct number, stop the loop and display a win message. Within the loop tell them if they are too high or too low and ask for the next guess. Finally, if the user enters display the random number and stop the loop essentially allowing them to quit the game
Example Output
Lets play Guess The Number
Whats your guess?
Too high!
Whats your guess?
Too low!
Whats your guess?
Too high!
Whats your guess?
That's right!
Practice
Use a loop and an array to generate a list of random numbers between the ranges of and inclusive. Then loop through the array again and print out all the numbers.
Example Output
Your random numbers are:
Practice
String types actually contain an array of char types inside of them. You can access this array of chars by using the array subscript operator the brackets on the string itself. Now that you know this, ask the user to enter a message and read it into a string variable. Then loop through the characters of the string backwards to print the message out in reverse.
Example Output
Please enter a message to reverse:
Hello, World!
Here is your message reversed:
dlroW olleH
Practice
Define an array of strings, initializing it with some names of your choice. Ask the user to specify which name they would like to search for. Next, search your array for the name the user specified, by iterating through the array and comparing their search term with each element of the array. Then let the user know if the name was found in the array or not. Finally, print out the array of strings so the user can verify.
Example Output
Which player would you like to look for: Luigi
Yes, Luigi is in the list!
Look for yourself:
Mario, Luigi, Peach, Toad,
Practice
Define and initialize an array of random sales figures floating point numbers Then iterate through the array of numbers and compute the following:
total sales
average sale
highest sale
lowest sale
Example Output
Sales:
Total:
Average:
Highest:
Lowest:
Practice
Time to play the Lotto! Define two arrays of unsigned short integers. One array is for the user's guesses and the other is for the lotto numbers. Using a for loop, randomize the five lotto numbers to be in the range: Using another for loop, ask the user for their five lotto picks. Then display the random lotto numbers to the user. To calculate the user's winnings, use a for loop and check if each element in the user's array is equal to a corresponding element in the lotto array useri lottoi Increase a winnings variable by for each matching number.
Example Output
Enter your pick for lotto ball #:
Enter your pick for lotto ball #:
Enter your pick for lotto ball #:
Enter your pick for lotto ball #:
Enter your pick for lotto ball #:
Lotto Numbers are:
You win $
Practice
Lets modify the previous lab to improve our chances of winning. Instead of checking for a matching number in the corresponding position, we could check if a user's pick matches any of the lotto numbers. So to calculate the winnings, write a for loop to iterate through the user's five numbers, and within that loop, write a nested for loop to iterate through the five lotto numbers NOTE: You must use different names for the loop counters. Both counters shouldnt be called i Increase the winnings variable by for each matching number or make the increase exponential if you want more money
Example Output
Enter your pick for lotto ball #:
Enter your pick for lotto ball #:
Enter your pick for lotto ball #:
Enter your pick for lotto ball #:
Enter your pick for lotto ball #:
Lotto Numbers are:
You win $
Practice
Write an extreme version of the Hello World program. Write the following function, which prints Hello, World! to the screen once:
static void PrintGreeting
Now, create an indefinite loop that simply calls your function over and over again each loop iteration. Note that you will have to hard kill this program since there is no stopping condition to your loop.
Example Output
Hello, World!
Hello, World!
Hello, World!
Practice
Write a power function which computes one value raised to the power of another and then returns the result ie fbase exp base exp The function header should look like this:
static int Powerint x int exp
Now, outside the function, perhaps in Main, ask the user for both a base and an exponent, with input validation, and call the function passing those inputs as arguments. Catch the return value of the function and display it on the screen.
Example Output
Base: ksdjf
Base:
Exponent:
raised to the power is
Practice
Write a function to convert degrees to radians. This function is useful because most System.Math library functions take radians as arguments, not degrees. Use the following conversion formula:
radians degrees
Once written, ask the user for some degrees, call your function to convert, and then display the result.
Example Output
Enter degrees: kdjfd
Enter degrees:
Radians:
Practice
Create a function to calculate a factorial. First, use input validation to ask the user for an integer, ensuring the user does not enter a negative value, floatingpoint value, or nonnumeric text. After the input validation, pass the integer to your function and within the function write a for loop to start the counter at the user's input, run as long as the counter is greater than or equal to decreasing the counter by each time. Within the loop, multiply the counter variable into a product variable that starts at product i Have the function return the factorial product. Main should display the final answer.
Example Output
What number would you like a factorial of five
What number would you like a factorial of
What number would you like a factorial of
Calculating
x x x x
Practice
Write a program to display the multiples of a number up to another number thus the second number should be greater than or equal to the first number The function itself should start at the first number and loop until reaching the second number. The function header should look like this:
static void Multiplesint number, int max
Outside the function, ask the user for two numbers, storing them into integer variables. Then call your function sending the user's numbers as inputs.
Example Output
Enter a number:
Enter a max:
The multiples of up to are:
Practice
Write a program to detect if a number is prime or not. Create a function that takes a number in as input, detects if it is prime, and then returns a boolean indicating if the number is prime or not. There are various ways to check if a number is prime, though the easiest way to understand is to simply loop through all values from number inclusive, and see if any of those values divide evenly into the number. If none of those values divide evenly into the number, then the number is prime.
Example Output
Enter a number to detect primality:
is prime!
Enter a number to detect primality:
is not prime!
Practice
Pic
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
