C Programs Tutorials | IT Developer
IT Developer

Java Programs



Share with a Friend

Menu Driven Program - Java Programs

Using the switch statement, write a menu-driven program to perform the following operations:
(i) To print the value of z where z = (x3 + 0.5x) / y where x ranges from -10 to 10 with an increment of 2 and y remains constant as 5.5.
(ii) To print the Floyd’s Triangle with N rows.
Example:
If N = 5
OUTPUT:

1
2    3
4    5    6
7    8    9    10
11   12   13   14   15
import java.util.*; class Menu{ public static void main(String args[]){ Scanner in = new Scanner (System.in); System.out.println("1. Print the value of z"); System.out.println("2. Floyd\'s Triangle with N rows"); System.out.print("Enter your choice: "); int choice = in.nextInt(); switch(choice){ case 1: System.out.print("y = "); double y = in.nextDouble(); for(int x = -10; x <= 10; x += 2){ double z = (Math.pow(x, 3) + 0.5 * x) / y; System.out.print(z + "\t"); } System.out.println(); break; case 2: int num = 1; System.out.print("N = "); int n = in.nextInt(); for(int i = 1; i <= n; i++){ for(int j = 1; j <= i; j++){ System.out.print(num + "\t"); num++; } System.out.println(); } break; default: System.out.println("Invalid choice!"); } } }

Output

 
 OUTPUT 1: 
1. Print the value of z
2. Floyd's Triangle with N rows
Enter your choice: 1
y = 5
-201.0  -103.2  -43.8   -13.2   -1.8    0.0 1.8 13.2    43.8    103.2   201.0    

 OUTPUT 2: 
1. Print the value of z
2. Floyd's Triangle with N rows
Enter your choice: 2
N = 5
1   
2   3   
4   5   6   
7   8   9   10  
11  12  13  14  15