Write a menu-driven program to perform the following (using a switch-case statement):
Java Programming Language (Article) (Program)
25
Write a menu-driven program to perform the following (Use switch-case statement):
- To print the series
0, 3, 8, 15, 24 ... n terms
(value ofn
is to be an input by the user). - To find the sum of the series given below:
S = 1/2 + 3/4 + 5/6 + 7/8 ... 19/20
Program:
import java.io.*; public class Menu { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int choice, n; double s = 0; System.out.println("Enter 1. To Print the Series"); System.out.println("Enter 2. To Print the Sum of Series"); System.out.println("Enter Your Choice"); choice = Integer.parseInt(br.readLine()); switch (choice) { case 1: System.out.println("Enter the number of terms:"); n = Integer.parseInt(br.readLine()); System.out.print("Series: "); for (int i = 1; i <= n; i++) { int term = i * i - 1; System.out.print(term + " "); } System.out.println(); break; case 2: for (int i = 1; i <= 19; i += 2) { s += (double) i / (i + 1); } System.out.println("Sum of series: " + s); break; default: System.out.println("Wrong Choice"); break; } } }
Output:
Explanation:
Name | Type | Description |
---|---|---|
choice | int | Stores the user's choice from the menu options. |
n | int | Stores the number of terms for the series in Task 1. |
s | double | Accumulates the sum of the series fractions in Task 2. |
i | int | Loop counter used in both series and sum calculations. |
term | int | Temporary variable used to store the computed term in the series for Task 1. |
br | BufferedReader | Reads input from the user. |
Explanation for Question 9
The program uses a menu-driven approach with a switch-case statement to perform two tasks based on user input.
Task 1: Print the Series
The first option in the menu allows the user to print a series of numbers. The series is defined as 0, 3, 8, 15, 24, ...
and the pattern is i * i - 1
, where i
is the term index. The user is prompted to enter the number of terms n
, and the program then calculates each term using the formula and prints the series.
Task 2: Sum of the Series
The second option calculates the sum of a series where each term is of the form i / (i + 1)
, for i
ranging from 1 to 19 with an increment of 2. The sum is computed by iterating through the terms and adding them up. The result is then printed out.
Program Breakdown
- The program starts by importing the necessary classes for input handling.
- The
BufferedReader
class is used to read user input from the console. - The
switch
statement determines which task to execute based on the user's choice. - For the series, a
for
loop generates and prints each term. - For the sum, another
for
loop calculates the sum of fractions. - The
default
case handles invalid choices by displaying an error message.
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.