Computer Science, asked by pankajsingh06051981, 10 months ago

Write a menu driven program to find the sum of the following series depending on the user choosing 1 or 2
1. S=1/4+1/8+1/12.........upto n terms
2. S=1/1!-2/2!+3/3!.......upto n terms
where ! stands for factorial of the number and the factorial value of a number is the product of all integers from 1 to that number, e.g. 5! = 1 2 3 4 5.
(use switch-case).​

Answers

Answered by ridhimakh1219
79

Write a menu driven program

Explanation:

Function to calculate sum of following series:

1. S=1/4+1/8+1/12.........upto n terms

void series_first(int n)

{

 int i,x=4;

 double s=0;

 for(i=0;i< n;i++)

  {

  s=s+(double)1/x;

  x=x+4;

  }

  System.out.println("Result="+s);

 }

Function to calculate sum of following series:

2. S=1/1!-2/2!+3/3!.......upto n terms

void series_second(int n)

{

 int i;

 double s=0;

 int j,f;

 for(i=1;i<=n;i++)

  {

  f=1;

  for(j=2;j<=i;j++)

  f=f*j;

  s=s+(double)1/f;

  }

  System.out.println("Sum of the series="+s);

 }

Output

Enter value of n:

6

Enter choice (1 or 2):

2  

Sum of the series=1.7180555555555557

Please find attached program

Attachments:
Answered by ranjan12342003
9

import java.io.*; class SwitchStatement { public static void main(String argsQ) throws IOException { InputStreamReader reader = new InputStreamReader(System.in); BufferedReader input = new BufferedReader(reader); System.out.println(“l-Sum of Series:”); System.out.println(“2-Display Special Series:”); System.out.println(“Enter your choice:”); String n1 = input.readLine( ); int ch = Integer.parselnt(nl); System.out.println(“Enter Number of Terms String t = input.readLine(); int n = Integer.parselnt(t); switch (ch) { case 1: int sign = -1; double term = 0; double sum = 0; int x = 2; System.out.println(“Value of x: ” +x); System.out.println(“Number of terms: ” +n); sum + = x; // First term added here, for (int i = 2; i < = n; i+ +){ term = sign * Math.pow(x,i); sum + = term; sign *= -1; } System.out.println(“Sum of Series +sum); break; case 2 : int num; System.out.println(“Enter the number of terms: ”); String tm = input.readLine(); . num = Integer.parselnt(tm); int s = 0, c; for (c = 1; c < = num; c+ +){ s = s * 10 + 1; System.out.print(s + ” “); } break; } } }

Similar questions