Problem Description: Displaying menu options and performing arithmetic operations based on user choice

For this Lab your program must perform three different arithmetic operations based on a users input. To do this, you must show the user the menu, ask the user for their choice, perform the appropriate computation, and then repeat until the user exits. The menu is as follows:

Please choose your choice from following menu:
1) Calculate the sum of integers 1 to m.
2) Calculate the factorial of given number.
3) Generate the 1st integer from left for a given number.
4) Quit.

Step 1: Getting Started:

Create a class called Lab4. Be sure to name your file Lab4.java.

Step 2: Beginning the loop and declaring variables

The first part of the problem is to show the user the menu and repeat the menu until the user selects the exit option.

For this, we need to declare a variable choice to store the user choice.

int choice; //initialize do-while loop

Next, we must select the appropriate loop for the main body of the program. In this case, since we know that the program should execute at least once, a do {} while loop is most appropriate. Inside the loop, print the menu using System.out.println statements and then request the menu option from the user. A skeleton do-while loop follows. Remember to add the Scanner statement that requests the choice from the user.

do{
//Please choose your choice from following menu
//1) Calculate the sum of integers 1 to m.
//2) Calculate the factorial of given number.
//3) Find the first digit of a given number.
//4) Quit.
} while(/* termination condition*/)

Remember every loop should have a termination condition. Here the loop should run until user chooses option 4. So the termination condition would be (choice != 4)

Step 3: Performing arithmetic operations

Based on the choice entered by the user, we need to do some arithmetic operations. Inside the do-while loop, but after the menu, initialize a switch-case statement to perform the operations. The switch-case statement hinges on the choice variable, which should now have an integer value. An example switch-case statement follows.

switch(choice):{
case 1:
break;
case 2:
break;
case 3:
break;
default:
break;
}

Step 4: CASE 1 arithmetic operation /* Code to be done inside CASE 1 statement */

If the user chooses option one, they wish to compute the sum of integers from one to a target number, m. Thus, we must request this target number from the user and sum up the integers from 1 to that number.

First, we declare two new integers, one for the target number and one for the sum.

int m,sum=0;

It is important to initialize the value of sum as 0, as or else the sum variable may start with an incorrect value.

Next, ask the user to enter a number using System.out.println and then use a Scanner object to get the value.

//-->

To perform the operation, we need an intermediate variable that goes from 1 to m in a loop and guides the sum. For that, initialize a variable. (These variable are called index variables and typically start at i and proceed in alphabetical order, i.e., i,j,k).

int i;

Begin a for loop using i and taking its values from 1 to m and inside the loop update the value of the sum in the following manner:

for (i=1,i<=m;i++)
{
sum=sum+i;
}/* at the end of each iteration sum will have the value of sum till ith integer*/

Print the value of sum so that user can see it using System.out.println.

Step 5: CASE 2 arithmetic operation /* Code to be done inside CASE 2 statement */

The user chooses option 2 in order to compute the factorial of given integer

For calculating the value of the factorial, we need to have two variables: n, the integer entered by the user, and fact, the value of the factorial.

int n,fact=1; /* ensure that initial value of fact is 1.*/

Ask the user to enter a number using System.out.println and a Scanner object.

To perform the operation, we need an intermediate variable that goes from 1 to m in a loop and guides the factorial. For that, initialize an index variable.

int i;

Begin a for loop using i and taking its values from 1 to n and inside the loop update the value of the factorial in the following manner:

for (i=1,i<=n;i++)
{
fact=fact*i;
}/* at the end of each iteration fact will have the product of 1st i integers*/

Print the value of fact using System.out.Println.

Step 6: CASE 3 arithmetic operation /* Code to be done inside CASE 3 statement */

The user chooses option 3 in order to get the first digit of a given number. To calculate this, we need to have two variables: num, the integer entered by the user, and rem, which stores the value of reminder when num is divided by 10. Ask the user to enter a number using System.out.println and store the value entered by the user in the num variable using a Scanner object.

The logic is that we will divide the number until the quotient becomes 0 and store the reminder at every iteration. At the end of each iteration, the reminder will have the 1st integer from the right form from the previous iteration.

We need to use while loop to perform the operation, since we dont know how big the number is ahead of times and the terminating condition would be num = 0. This logic will be explained during lab hours.

while(num!=0) {
rem=num%10;
num=num/10;
}/* at every iteration rem will have the 1st digit from left for an integer */
/* num will have 1 digit less than its previous iteration */

Step 7: Display the output

You should display the answer of the CASE conditions as mentioned and repeat the menu operations.

Sample Output

Below is an example of what your output should roughly look like when this lab is completed. Text in bold represents user input.

Please choose from following menu
1. Calculate the sum of 1 to m integers
2.Calculate the factorial of given number
3.Generate the 1st Integer from left for given number
4.Quit
1
Enter the number
12

The sum of first 12 numbers is 78

Please choose from following menu
1. Calculate the sum of 1 to m integers
2.Calculate the factorial of given number
3.Generate the 1st Integer from left for given number
4.Quit
2
Enter the number
5

The factorial of 5 is 120

Please choose from following menu
1. Calculate the sum of 1 to m integers
2.Calculate the factorial of given number
3.Generate the 1st Integer from left for given number
4.Quit
3

Enter the number
987654321
The 1st integer of given number from left is 9

Please choose from following menu
1. Calculate the sum of 1 to m integers
2.Calculate the factorial of given number
3.Generate the 1st Integer from left for given number
4.Quit
4
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.