Home / Programs / Question 7
Programming Example

Question 7

👁 99 Views
💻 Practical Program
📘 Step by Step Learning
Study this program carefully to understand the logic, output, and explanation in a structured way.

Information & Algorithm

Question 7

Using the switch statement, write a menu driven program to perform 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 at 5.5.

(ii) To print the Floyds triangle with N rows
Example: If N = 5, Output:
1
2 3
4 5 6
7 8 9 10
11 12 13 14

Program Code

import java.util.Scanner;

public class RansariMenu
{
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.println("Type 1 for Value of Z");
        System.out.println("Type 2 for Floyd\'s triangle");
        System.out.print("Enter your choice: ");
        
        int choice = in.nextInt();
        
        switch (choice) {
            case 1:
            double Z, Y = 5.5;
            for (int x = -10; x <= 10; x+= 2) {
                Z = (Math.pow(x, 3) + 0.5 * x) / Y;
                System.out.println("Value of Z when x is "
                        + x + " = " + Z);
            }
            break;
            
            case 2:
            System.out.print("Enter number of rows: ");
            int N = in.nextInt();
            int t = 1;
            for (int i = 1; i <= N; i++) {
                for (int j = 1; j <= i; j++) {
                    System.out.print(t + " ");
                    t++;
                }
                System.out.println();
            }
            break;
            
            default:
            System.out.println("Incorrect Choice");
            break;
        }
    }
}

How to learn from this program

First read the algorithm, then study the program code line by line. After that, compare the code with the output and finally go through the explanation. This approach helps learners understand both the logic and the implementation properly.