Hello my friends at Chegg, I\'m in need of your help once again writing a java p
ID: 3847461 • Letter: H
Question
Hello my friends at Chegg, I'm in need of your help once again writing a java program. I don't know what it is, but this java stuff has me lost in the sauce, so I'm grateful fo you guys at Chegg. That said I need to write a program using a Switch/Case block that executes depending upon menu choice, and use a for loop to determine and output the data, and lastly Implement a do...while loop to run the program again.
In mathematics, the factorial of a positive integer n^1 (written as n!) is the product of all positive integers less than or equal to n. For example:
5! = 5 * 4 * 3 * 2 * 1 = 120
This week, you will write a simple program that will determine factorial values of positive integers.
Write a program that will determine the factorial values of integers starting with the number 1 and display all factorials up to a user inputted value.
Constraints
Factorials and their results are displayed as such: 4! = 24
Requirements
Display an opening welcome statement and menu as such:
Welcome to my factorial program!
Please choose from the following:
1. Run Program
2. Exit Program
Use a Switch/Case block that executes depending upon menu choice
Use a for loop to determine and output the data
Implement a do...while loop to run the program again
This program will utilize code learned from Week 1 through Week 5
Hints
As you can see by the example output below, the factorials can grow exponentially so be mindful of this when you make your variable declarations (Google BigInteger)
Notice that the factorial begins at 1 and there are 10 values returned when the user requested 10. This requested number can be larger or smaller depending on user input
Notice how some of the output instructions, welcome, menu, etc. are not repeated in the second run
Expected Output
On the next page, you will find an output sample for two runs of the program. User input is in BOLD
Run 1:
Welcome to my factorial program! Please choose from the following:
1. Run Program
2. Exit Program
:1
This program will determine the factorial value of positive integers.
The starting number is 1.
Please enter an ending integer value: 10
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
6! = 720
7! = 5040
8! = 40320
9! = 362880
10! = 3628800
Run factorial program again? (Y for Yes, N for No): y
Run 2: (User entered y above to run the program again)
The starting number is 1.
Please enter an ending integer value: 5
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
Run factorial program again? (Y for Yes, N for No): n
Thank you for using the factorial program! Goodbye!
Explanation / Answer
import java.util.*;
class FactorialTest
{
public static void main (String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Welcome to my factorial program! Please choose from the following:");
System.out.println("1. Run Program");
System.out.println("2. Exit Program");
int option = scan.nextInt();
String Cont ="C";
switch(option)
{
case 1 :
System.out.println("This program will determine the factorial value of positive integers.");
do
{
System.out.println("The starting number is 1.");
System.out.println("Please enter an ending integer value:");
int n = scan.nextInt();
for(int i=1;i<=n;i++)
{
System.out.println(i+"! = "+fact(i));//call to function
}
System.out.println("Run factorial program again? (Y for Yes, N for No): ");
Cont = scan.next();
if(Cont.equals("N"))
break;
}while(Cont.equals("Y"));// do while loop
break;
case 2:
System.exit(0);
default: System.out.println("Invalid option");
break;
}
}
static long fact(int x)
{
long f = 1;
for(int i=1;i<=x;i++)
{
f = f*i;
}
return f;
}
}
Output:
Welcome to my factorial program! Please choose from the following:
1. Run Program
2. Exit Program
1
This program will determine the factorial value of positive integers.
The starting number is 1.
Please enter an ending integer value:10
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
6! = 720
7! = 5040
8! = 40320
9! = 362880
10! = 3628800
Run factorial program again? (Y for Yes, N for No): Y
The starting number is 1.
Please enter an ending integer value:5
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
Run factorial program again? (Y for Yes, N for No): N
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.