In: Computer Science
Programming Problem
Design a program in java 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
PLEASE USE THE FOLLOWING FORMAT
I need it in the format of a do while loop
Due today please help!
public class Lab4 {
public static void main(String[] args) {
// Declare some variables you need
// -->
do {
// Display the menu
displayMenu();
// Ask the user for one option
// -->
switch (?????) {
// Define four cases for different options. Don't forget
"break".
// -->
}
} while (?????);
}
/**
* Print the menu
*/
private static void displayMenu() {
System.out.println("Please choose one option 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) Display the leftmost digit of a given
number");
System.out.println("4) Quit");
}
}
If you can explain that would be awesome, thank you!
import java.util.*;
import java.util.Scanner;
//main class
public class Lab4 {
//function displays the menu
private static void displayMenu()
{
System.out.println("Please choose one option 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) Display the leftmost digit of a given
number");
System.out.println("4) Quit");
}
//main function
public static void main(String[] args)
{
//scanner
Scanner sc = new Scanner(System.in);
//varibles declaration
int input = 0,sum=0,fact=1,number;
String str1,str2;
char arr[] = new char[200];
//do while loop
do
{
// Display the menu
//calling the function
displayMenu();
// Ask the user for one option
//reads the input from user
input = sc.nextInt();
//switch case
switch (input)
{
// Define four cases for different options. Don't forget
"break".
// -->
//case 1 is sum of number from 1 to number
case 1:
System.out.println("Enter a Number: ");
//reads the number from user
number = sc.nextInt();
//runs the loop tocalculate the sum of numbers
for(int i=1;i<=number;i++){
sum = sum+i;
}
//prints the sum of integers
System.out.println("Sum of Integers: "+sum);
//breaks the switch block
break;
//case 2 factorial of anumber
case 2:
System.out.println("Enter a Number: ");
//reads the input from user
number = sc.nextInt();
//runs the loop to calculate the factorial of a number
for(int i=1;i<=number;i++){
fact*=i;
}
//prints the factorial of number
System.out.println("The factorial of "+number+" is "+fact);
break;
//case3 leftmost of number
case 3:
System.out.println("Enter a Number: ");
//reads the input
number = sc.nextInt();
// number is converting to string by using toString()
function
str1 = Integer.toString(number);
//converting string to char array by using toCharArray();
arr = str1.toCharArray();
//printst the left most bit by index value of array
System.out.println("The leftmost most digit of: "+number+" is
"+arr[0]);
break;
//case 4
case 4:
System.out.println("Bye");
break;
}
}//while loop fails when input is 4
while (input!=4);
}
}
OUTPUT