Home Java Programming Language / Programs / Question 7
🚀 Programming Example

Question 7

👁 101 Views
💻 Practical Program
📘 Step Learning
Learn this program step-by-step with algorithm, source code, output and detailed explanation.

📌 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;
        }
    }
}
                        
📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.

🔥 Practice suggestion

Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.