Write a menu driven program to perform the
following:
(i) To print the series 0,3,8, 15, 24.... n teams (value of 'n' is to be an input by the user)
ii) To find the sum of the series given below:
S = 1/2+3/4+5/6+ 7/8....
19/20
Write The Program In Java and can anyone pls give the answer as quickly as possible
Answers
Required Answer:-
Question:
Write a menu driven program to perform the following tasks.
- Print the series 0 3 8 15 upto n terms.
- Find the sum of the series - ½ + ¾ + . . . . . + 19/20.
Solution:
Here comes the códe.
import java.util.*;
public class MenuDrivenProgram {
public static void main(String[] args) {
int n, i, a, b;
double x, y, s=0.0;
Scanner sc=new Scanner(System.in);
System.out.println("1. Display series.");
System.out.println("2. Calculate series sum.");
System.out.print("Enter your chóice: ");
switch(sc.nextInt()) {
case 1:
System.out.print("Enter limit: ");
n=sc.nextInt();
for(i=1,a=0,b=3;i<=n;i++) {
System.out.print(a+" ");
a+=b;
b+=2;
}
break;
case 2:
for(x=1.0,y=2.0;x<=19.0;x+=2,y+=2)
s+=x/y;
System.out.println("Sum: "+s);
break;
default: System.out.println("Invalid Chóice.");
}
sc.close();
}
}
Explanation:
- This is solved using switch case. If the user enters valid chóice, then it will perform the given task as per the chóice given or else, displays an error message.
See the attachment for output ☑.