Computer Science, asked by BrainlyPromoter, 1 year ago

Write a java program to find the sum of following series using a method:

1 +  \frac{x}{1} ! +  \frac{ {x}^{2} }{2} ! +  \frac{ {x}^{3} }{3}! + ....... +  \frac{ {x}^{n} }{n}

• The program should be menu driven.


Anonymous: upto (x^n/n)! or (x^n!)n ?
BrainlyPromoter: (x^n/n)!
queenvictoria48: hey plzz delete my account plzz

Answers

Answered by Anonymous
9

\huge{\textsf{JAVA CODE :}}


import java.util.*;

class series_sum

{

public static void main(String args[ ])

{

Scanner sc=new Scanner(System.in);

System.out.println("Enter 1 to view the output");

int ch=sc.nextInt();

switch(ch)

{

case 1:

System.out.println("Enter the limit");

int n=sc.nextInt();

System.out.println("Enter the value of the numerator");

int x=sc.nextInt();

int s=1;

int f=1;

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

{

f=1;

int a =(int)(Math.pow(x,i))/i;

for(int j=1;j<=a;j++)

{

f=f*j;

}

s=s+f;

}

System.out.println("The sum of the series is = "+s);//print

break;

default:

System.out.println("Invalid choice");

}//switch case

}//end of main

}//end of class


----------------------------------------------------------------------------


\textsf{\huge{\underline{NOTE}}}


If you prefer to print in (integer) type


(i) Firstly , if you understand concepts of programming then the most important fact that you should note is the line :

int a =(int)(Math.pow(x,i))/i;

Here I have used (int) to use explicit typecasting. Why so ?

⇒ First of all : Math.pow is the function involving double datatype

⇒ We know that double datatype has higher memory than int datatype

⇒ Whenever such a case appears always do typecasting

⇒ Otherwise an error will show  during compiling .

⇒ Note that this will be approximate as it is integer


If you want to print in double type


1. Make these changes :

  • First of all , make i as double
  • Make j as double
  • Make s as double
  • Make f as double
  • Offcourse a too has to be double
  • Only leave out n
  • And also leave out on that ch

2. Also change every 1 with 1.0 and don't make errors .


(ii) Next thing to note is that I have calculated the factorial using the variable f.

(iii) I have found out the Sum using variable s which is given an initial value of 1 .This is done so that it adds 1 with all the other factorials calculated.

(iv) Note that the print statement is outside the for-loop. Do this whenever you need to find the sum of any series

(v)There was no need for it to be menu-driven . Please comment if you need anything else

(vi) Don't forget to use break and default case.

(vii) If you use updated compilers , don't use static in the void main()


Hope it helps !

________________________________________________________________

Answered by siddhartharao77
9

Sample Program to find the series:

import java.util.*;

class Brainly

{

public static void main(String args[])

{

float x,b=1,c=1;

int n,select=1;

Scanner demo = new Scanner(System.in);

switch(select)

{

case 1:

System.out.println("Enter the value of x: ");

x = demo.nextFloat();

System.out.println("Enter the value of n: ");

n = demo.nextInt();

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

{

c = c*x/(float)i;

b=b+c;

}

System.out.println("The sum is:" +b);

break;

default:

System.out.println("Invalid");

}

}

}


Output:

Enter the value of x:4

Enter the value of n:6

The sum is 42.86667.



Hope it helps!

Attachments:
Similar questions