Question
Objectives: Loops Validating the range switch statement Program : Write a C program to prompt the user for a whole number between 3 to 10.
Objectives:
Loops
Validating the range
switch statement
Program: Write a C program to prompt the user for a whole number between 3 to 10. Present a menu of options and prompt the user to select an option. Use the switch statement to carry out the selected option. The menu of options is:
Menu of Options:
Display all the numbers from 1 to num.
Display all the even numbers from 1 to num.
Compute the sum of 1 + 22 + 32 + . num2.
Display numbers in reverse order from num to 1.
Compute num!
End the program.
Follow the steps in the main function.
int main()
{
// declare variables:
int num;
int option = 0;
// declare more variables as needed.
// Prompt the user for a whole number.
Validation: Validate the range of the users input in a loop (while loop or do-while loop). Keep prompting the user until the user enters a number between 3 and 10 both inclusive.
// Display the menu of options (see above).
// Prompt the user to select an option. Read it into variable option.
// Use the switch statement to process the selected option.
// Option-1: Write a loop to display numbers from 1 to num..
// Option-2: Write a loop to display the even numbers from 1 to num.
// Option-3: Write a loop to add up squares of all the numbers from 1 to num. Display the sum.
// Option-4: Display numbers in reverse order, num to 1.
// Option-5: Compute num factorial = 1 * 2 * * num
// Option-6: Thank the user and end the program.
// default: display Invalid option.
_getch();
return 0;
}
BONUS: Rerun the program so that the user can choose different options for the same input number. The rerun stops when the user selects option #6.
Introductory Comments:
At the beginning of the source code, include the following information:
/*=======================================================
Programmer: Your name
Email: Your email
CNIT 105, Assignment05
Program Description: A brief description in your own words.
==========================================================*/
Submission:
Before submission, get rid of all the build errors. Do not worry about the warnings. Test your program with different input data and choosing different options. When ready, close the project. Submit the source file (.c file) to the provided link on BB.
Grading chart:
Points | Criteria |
Intro. Comments as specified. (-1 if missing) | |
Variable declarations | |
2 | Prompt - read a whole Validate the range in a loop = 2 |
2 | Display the menu of options. |
Prompt the user and read the selected option | |
16 | switch statement to process the option Option-1 = 2 Option-2 = 2 Option-3 = 3 Option-4 = 3 Option-5 = 3 Option-6 = 1 Default = 2 |
BONUS | Rerun the program = 2 |
Penalties: Programs with compile errors will not be graded.
Screen Capture:
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started