Your pogram should present a menu with 4 options. Your program should only exit
ID: 3577679 • Letter: Y
Question
Your pogram should present a menu with 4 options.
Your program should only exit when the user enters a menu selection of 0 (or kills the process)
When the user selections menu option 1: Ask the user for a maximum value and print out a multiplication table for values 0 to max.
Example, value 4 chosen:
0 1 2 3 4
0 0 0 0 0 0
1 0 1 2 3 4
2 0 2 4 6 8
3 0 3 6 9 12
4 0 4 8 12 16
Menu option 5 chosen. Ask the user for a base and exponent value. Calculate the resulting value and print: <base>^<exponent> = <result>
For example if the user enters a base of 6 and an exponent of 4:
6^4 = 1296
Menu option 6 chosen. Ask the user for a value and then calculate and display the result of taking the factorial of thaat value.
Example, value 4 chosen print the folloing:
4! = 24
If the user enters another value print "Invalid selection" to the screen
Update your menu with the new features.
Explanation / Answer
package dec13;
import java.util.Scanner;
public class CalculateMath {
public static void main(String[] args) {
System.out.println("Enter any number: ");
Scanner in = new Scanner(System.in);
int choice = in.nextInt();
if (choice == 0) {
System.exit(0);
} else if (choice == 1) {
System.out.println("Enter any Max number: ");
int num = in.nextInt();
for (int i = 0; i <= num; i++) {
System.out.print(" " + i);
}
System.out.println();
for (int i = 0; i <= num; i++) {
System.out.print(i + " ");
for (int j = 0; j <= num; j++) {
System.out.print(i * j + " ");
}
System.out.println();
}
} else if (choice == 5) {
System.out.println("Enter base number: ");
int base = in.nextInt();
System.out.println("Enter exponent number: ");
int exp = in.nextInt();
int ans = (int) Math.pow(base, exp);
System.out.println(base + " ^ " + exp + " = " + ans);
} else if (choice == 6) {
System.out.println("Enter number: ");
int number = in.nextInt();
int i, fact = 1;
for (i = 1; i <= number; i++) {
fact = fact * i;
}
System.out.println("!" + number + " = " + fact);
} else
System.out.println("Invalid Selection");
}
}
-----------------------------
output:
Enter any number:
1
Enter any Max number:
5
0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 1 2 3 4 5
2 0 2 4 6 8 10
3 0 3 6 9 12 15
4 0 4 8 12 16 20
5 0 5 10 15 20 25
Enter any number:
5
Enter base number:
6
Enter exponent number:
4
6 ^ 4 = 1296
Enter any number:
6
Enter number:
4
!4 = 24
Enter any number:
2
Invalid Selection
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.