$29
CSE 110 - Lab 4
Lab Topics
● More practice with loops
● Switch statement
● Performing arithmetic operations inside the loop
Programming Problem
In this lab, you need to design a program that can perform three different arithmetic operations
based on the user’s input. The three operations are
1. Summation of integers from 1 to m
2. Factorial of a given number n (n!)
3. Finding the leftmost digit of a given integer (note: you have to use int)
This program should prompt the user a menu including four options, ask the user for one option,
and perform the corresponding computation. This process will repeat until the user chooses
“Quit”.
Please use this menu in your program: (use a switch statement)
Please choose one option from the following menu:
1) Calculate the sum of integers from 1 to m
2) Calculate the factorial of a given number
3) Display the leftmost digit of a given number
4) Quit
Sample Output
Below is an example of what your output should roughly look like when this lab is completed.
Text in RED represents user input.
Please choose one option from the following menu:
1) Calculate the sum of integers from 1 to m
2) Calculate the factorial of a given number
3) Display the leftmost digit of a given number
4) Quit
1
Enter a number:
4
The sum of 1 to 4 is 10
Please choose one option from the following menu:
1) Calculate the sum of integers from 1 to m
2) Calculate the factorial of a given number
3) Display the leftmost digit of a given number
4) Quit
2
Enter a number:
5
The factorial of 5 is 120
Please choose one option from the following menu:
1) Calculate the sum of integers from 1 to m
2) Calculate the factorial of a given number
3) Display the leftmost digit of a given number
4) Quit
3
Enter a number:
987654321
The leftmost digit of 987654321 is 9
Please choose one option from the following menu:
1) Calculate the sum of integers from 1 to m
2) Calculate the factorial of a given number
3) Display the leftmost digit of a given number
4) Quit
4
Bye
Program Design
The following flowchart shows a skeleton of this lab program. You can declare as many
variables as you like (omitted in the flowchart for readability). The processes asking the user
for inputs should come with appropriate prompt messages (e.g., print “Please enter a number”
before each option).
This program involves multiple conditions for optional computations. You must use a switch
statement to implement the menu rather than using if-else statement. Switch statement are
appropriate for menus and it helps with the logic of the code.
The program will stop when the user inputs an integer number 4. As described in the flowchart,
the loop condition is situated at the end of the loop process. Therefore, you have to use a dowhile statement to implement this logic.
Please see the template Lab4.java for the detailed instruction.
Make sure to upload your Lab4.java
Please submit your Lab4.java ONLY to “Lab 4 Upload Link” in Weekly Labs section. Do not
include any Eclipse project files.