C Programs Tutorials | IT Developer
IT Developer

Java Programs



Share with a Friend

Menu Driven - Series Program - Java Programs

Write a menu-driven program to perform the following (use switch case statement):
(a) To print the series 0, 3 7, 15, 24, … n terms (value of ‘n’ is to be an input by the user)
(b) To find the sum of the series given below:
S = 1 / 2 + 3 / 4 + 5 / 6 + 7 / 8 + … + 19 / 20.

import java.util.Scanner; class Menu{ public static void main(String args[]){ Scanner in = new Scanner(System.in); System.out.println("1. Series 1"); System.out.println("2. Series 2"); System.out.print("Enter your choice: "); int choice = Integer.parseInt(in.nextLine()); switch(choice){ case 1: System.out.print("n = "); int n = Integer.parseInt(in.nextLine()); for(int i = 1; i <= n; i++) System.out.print((i * i - 1) + " "); System.out.println(); break; case 2: double sum = 0.0; for(int i = 1; i <= 19; i++) sum += i / (i + 1.0); System.out.println("Sum = " + sum); break; default: System.out.println("Invalid choice!"); } } }

Output

 
 OUTPUT 1: 
1. Series 1
2. Series 2
Enter your choice: 1
n = 10
0 3 8 15 24 35 48 63 80 99 

 OUTPUT 2: 
1. Series 1
2. Series 2
Enter your choice: 2
Sum = 16.40226034285632