Question
Design a class to overload a function SumSeries() as follows:
(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)
Share code with your friends
Share on whatsapp
Share on facebook
Share on twitter
Share on telegram
Code
public class program7
{
public void SumSeries(int n, double x)
{
int i=0;
double sum = 0;
for (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,i=0,product=1,j=0;
for (i = 1; i <= 20; i++)
{
product = 1;
for (j = 1; j <= i; j++)
{
product = product * j;
}
sum = sum + product;
}
System.out.println("Sum = " + sum);
}
}