Design a class to overload a function SumSeries() as follows: [15]
(i) void SumSeries(int n, double x) – with one integer argument and one double argument to find and display the sum of the series given below: s = (x/1) – (x/2) + (x/3) – (x/4) + (x/5) … to n terms
(ii) void SumSeries() – To find and display the sum of the following series: s = 1 + (1 X 2) + (1 X 2 X 3) + … + (1 X 2 X 3 X 4 X … 20)
Answers
Answered by
64
Ans.
public class Series {
public void SumSeries(int n, double x) {
double sum = 0;
for (int i = 1; i <= n; i++) {
if (i % 2 == 1) {
sum = sum + (x / i);
} else {
sum = sum - (x / i);
}
}
System.out.println("Sum = " + sum);
}
public void SumSeries() {
int sum = 0;
for (int i = 1; i <= 20; i++) {
int product = 1;
for (int j = 1; j <= i; j++) {
product = product * j;
}
sum = sum + product;
}
System.out.println("Sum = " + sum);
}
}
T4Talent:
Tysm dear
Answered by
54
import java.util.*;
class Choice
{
void SumSeries(int n,double x)
{
double sum=0.0;
int i;
(for i=1;i<=n;i++)
{
if(i%2==0)
sum=sum-(double)x/i;
else
sum=sum+(double)x/i;
}
System.out.println("Sum of first series:"+sum);
}
void SumSeries()
{
int i,p=1,sum1=0;
for(i=1;i<=20;i++)
{
p=p*i;
sum1=sum1+p;
}
System.out.println("Sum of second series:"+sum1);
}
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int n;
double x;
System.out.println("enter last limit of the series");
n=in.nextInt();
System.out.println("enter the value of x");
x=in.nextDouble();
Choice ob=new Choice();
ob.SumSeries(n,x);
ob.SumSeries();
}
}
class Choice
{
void SumSeries(int n,double x)
{
double sum=0.0;
int i;
(for i=1;i<=n;i++)
{
if(i%2==0)
sum=sum-(double)x/i;
else
sum=sum+(double)x/i;
}
System.out.println("Sum of first series:"+sum);
}
void SumSeries()
{
int i,p=1,sum1=0;
for(i=1;i<=20;i++)
{
p=p*i;
sum1=sum1+p;
}
System.out.println("Sum of second series:"+sum1);
}
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int n;
double x;
System.out.println("enter last limit of the series");
n=in.nextInt();
System.out.println("enter the value of x");
x=in.nextDouble();
Choice ob=new Choice();
ob.SumSeries(n,x);
ob.SumSeries();
}
}
Similar questions
Math,
8 months ago
Hindi,
8 months ago
Math,
1 year ago
Social Sciences,
1 year ago
History,
1 year ago