Question 7

Java Programming Language (Article) (Program)

19

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:

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;
        }
    }
}

Output:


                                                                     
                              

This Particular section is dedicated to Programs only. If you want learn more about Java Programming Language. Then you can visit below links to get more depth on this subject.