Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

So I alrady started the code, I\'m just having a hard time with the do, while st

ID: 3751858 • Letter: S

Question

So I alrady started the code, I'm just having a hard time with the do, while statment. It reads an error at the very end of my program when I write "{while (choice != 4); "

It states that choice cannot be resolved as a varibale and I'm so confused. I'll attach my code to this file and I've also attached the gruidlines for this lab as well.

import java.util.Scanner;

public class Lab4 {

public static void main (String[] args)

{

   Scanner scan = new Scanner(System.in);

  

   boolean flag = true;

  

   while (flag)

  

   do

   {

   System.out.println("Please Choose From the Following Menu");

      

   System.out.println("1. Calculate the sum of m integers from 1 to m");

  

   System.out.println("2. Calculate factorial of given number");

  

   System.out.println("3. Generate the first integer from the left for given number");

  

   System.out.println("4. Quit");

  

   int choice = scan.nextInt();

  

   System.out.println("Enter number");

  

   int num = scan.nextInt();

  

   int i = 1;

  

   switch (choice)

   {

  

   case 1:

       int m = 1, sum = 0;

       while ( i <= m) {

           sum = sum + i;

       i++ ;

       }

      

   System.out.println("Sum of numbers from 1 to " + m + "is" +sum);

  

   break;

  

   case 2:

       int n, fact = 1;

       i = 1;

       while (i <= n) {

           fact = fact * i;

           i++;

       }

      

   System.out.println("Factorial of Given Number: " +fact);

  

   break;

  

   int rem = num;

  

   case 3:

       num = 0;

       while(num != 0) {

           rem = num % 10;

           num = num / 10;

           }

       break;

      

       System.out.println("Left Digit of Given Number is " +rem);

          

   default:

       flag = false;

   }

   } while (choice != 4);

}

}

CSE 110 - Lab 4 What this Lab Is About: Getting familiar with loops Performing arithmetic operations inside the loop Use the following Coding Guidelines When declaring a variable, you usually want to initialize it. Remember you cannot initialize a number with a string .Remember variable names are case sensitive. Use tabs or spaces to indent code within blocks (code surrounded by braces). This includes classes, methods, and code associated with ifs, switches and loops. Be consistent with the number of spaces or tabs that you use to indent. Use white space to make your program more readable. Use comments after the ending brace of classes, methods, and blocks to identify to which block it belongs. 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 user's 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 the following menu: 1) Calculate the sum of integers 1 to m 2) Calculate the factorial of given number 3) Display the 1st integer from left for a given number. ) Quit.

Explanation / Answer

SumOfNos1ToM.java

import java.util.Scanner;

public class SumOfNos1ToM {

public static void main(String[] args) {

//Scanner Object is used to get the inputs entered by the user

Scanner sc = new Scanner(System.in);

  

//Declaring variables

int choice, m, sum = 0;

  

//This do-while executes until the user chooses the choice Quit(3)

do {

//Displaying the menu.

System.out.println(" Please Choose your choice from the Following menu");

System.out.println("1.Calculate the sum of integers from 1 to m");

System.out.println("2.Calculate the factorial of a given number");

System.out.println("3.Find the First of the given number");

System.out.println("4.Quit ");

choice = sc.nextInt();

switch (choice) {

//If the user chooses 1,Then case 1 will get executed.

case 1: {

  

//Declaring local variable

int i = 1;

  

//Getting the Number entered by the user

System.out.println("Enter the number :");

m = sc.nextInt();

  

//This loop will calculate the sum of the numbers from 1 to m

while (i <= m) {

sum = sum + i;

i++;

  

}

  

//Displaying the sum of the numbers from 1 to m

System.out.println("The Sum of the first "+m+" numbers is "+ sum);

break;

}

case 2:{

int n;

//Getting the input entered by the user

System.out.print("Enter a number :");

n=sc.nextInt();

  

//calling the method

int fact=calFactorial(n);

  

//Displaying the output

System.out.println("The factorial of "+n+" is "+fact);

break;

}

  

//If the user chooses 2,Then case 2 will get executed.

case 3: {

  

//Declaring variables

int num, rem = 0;

  

//Getting the number entered byt the user

System.out.print("Enter a number :");

num = sc.nextInt();

  

//This while loop will get the first integer of the given number

while (num != 0) {

rem = num % 10;

num = num / 10;

}

  

//Displaying the first integer of the given number

System.out.println("First Integer of the the given number from left is "+ rem);

break;

}

  

//If the user chooses 3,Then case 3 will get executed.

case 4: {

break;

}

  

//If the user chooses other than 1 or 2 or 3,Then default will get executed.

default: {

System.out.println(":: Invalid Choice ::");

}

}

} while (choice != 4);

}

//This method will calculate the factorial of a number

private static int calFactorial(int n) {

int res=1;

for(int i=1;i<=n;i++)

{

res*=i;

}

return res;

}

}

___________________

Output;


Please Choose your choice from the Following menu
1.Calculate the sum of integers from 1 to m
2.Calculate the factorial of a given number
3.Find the First of the given number
4.Quit

1
Enter the number :
4
The Sum of the first 4 numbers is 10

Please Choose your choice from the Following menu
1.Calculate the sum of integers from 1 to m
2.Calculate the factorial of a given number
3.Find the First of the given number
4.Quit

2
Enter a number :5
The factorial of 5 is 120

Please Choose your choice from the Following menu
1.Calculate the sum of integers from 1 to m
2.Calculate the factorial of a given number
3.Find the First of the given number
4.Quit

3
Enter a number :987654321
First Integer of the the given number from left is 9

Please Choose your choice from the Following menu
1.Calculate the sum of integers from 1 to m
2.Calculate the factorial of a given number
3.Find the First of the given number
4.Quit

4

________Could you plz rate me well.Thank You

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote