Objectives

  • Make use of multiple functions
  • Use Menus to select different functions

Instructions

Main Program:

Design your main program so that it has a while loop that will run until the user chooses to exit. Inside this loop, you will also need to prompt the user to ask them which function they would like to run. Use a switch statement to call the functions. This will be very similar to the menu that was created in Lab 4 (calculator.)

print the menu for three options
input the code
while code != 0
switch code
case 1: input n
call primeNumber function
break
case 2: input number of times
call weightedCoin function
break
case 3: enter a number
call evenOdd function
break
default: print “Error”
print the menu for three options
input the code
print “Coded by _______”
stop

Prime Numbers Function:

For the prime number function, have the method print out all primes up to a value n, which will be input from the keyboard by the user. You should ask for n inside of the main function and then pass it into the prime number method/function. Ex: n = 12 would print 1, 2, 3, 5, 7, 11

You will receive EXTRA CREDIT (+5) if the prime number method prints n number of prime numbers rather than prime numbers up to a value n. Ex: n = 12 would print 1, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31

Weighted Coin Function:

For this function, you will be simulating a coin toss. However, you will not be doing just any coin toss. The coin you will be ‘simulating’ will be weighted such that it will land on heads 60 percent of the time or the coin will land on tails only 40 percent of the time. In main, you need to have the user input the number of coin tosses he/she wishes to perform. Make sure the method prints out:

The total number of times the coin landed on heads. 2) The total number of times the coin landed on tails. 3) The percentage of the time the coin landed on heads.

Even or Odd Function:

In this function, you have one parameter, an integer. You need to be able to determine if the number is even or odd. Print out the appropriate message. Hint: What operator (+ - * / %) would be helpful here?

Run:

First select the prime generator function and put in a number for n between 10 and 20. Once it finishes, then select the coin toss function. Do the coin toss three times: 100, 500, and 1000. Thirdly, do the test for even or odd – enter three numbers into this function. Show one ‘error message’ (picking an invalid option.) Make sure to add “coded by_______” to the program (meaning you exited the program fully).

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.