(15)
Question 7.
Using the switch statement, write a menu driven program to perform following
operations:
(i)
To Print the value of Z where Z=x^3 +0.5x where x ranges from - 10 to 10 with
an increment of 2 and Y remains constant at 5.5.
(ii)
To print the Floyds triangle with N rows
Example: If N = 5, Output:
1
23
4 5 6
7 8 9 10
11 12 13 14 15
Answers
Explanation:
(i) import java.io.*;
class Menu{
public static void main(String args[])throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(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 = Integer.parseInt(br.readLine());
switch(choice){
case 1:
System.out.print("y = ");
double y = Double.parseDouble(br.readLine());
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 = Integer.parseInt(br.readLine());
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!");
}
}
}
(ii) import java.util.Scanner;
public class KboatPattern
{
public void choosePattern() {
Scanner in = new Scanner(System.in);
System.out.println("Type 1 for Floyd's triangle");
System.out.println("Type 2 for an ICSE pattern");
System.out.print("Enter your choice: ");
int ch = in.nextInt();
switch (ch) {
case 1:
int a = 1;
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(a++ + "\t");
}
System.out.println();
}
break;
case 2:
String s = "ICSE";
for (int i = 0; i < s.length(); i++) {
for (int j = 0; j <= i; j++) {
System.out.print(s.charAt(j) + " ");
}
System.out.println();
}
break;
default:
System.out.println("Incorrect Choice");
}
}
}
HOPE IT HELPS!! ✨