Question
Design a class to overload a function series() as follows:
(i) double series(double n) with one double argument and returns the sum of the series.
sum = 1/1 + 1/2 + 1/3 + ….. 1/n
(ii) double series(double a, double n) with two double arguments and returns the sum of the series.
sum = 1/a2 + 4/a5 + 7/a8 + 10/a11 ….. to n terms
Share code with your friends
Share on whatsapp
Share on facebook
Share on twitter
Share on telegram
Code
public class Overload
{
public double series(double n)
{
double s= 0;
for (int i = 1; i <= n; i++)
{
s = s + (1.0 / i);
}
return s;
}
public double series(double a, double n)
{
double s = 0;
for (int i = 0; i < n; i++)
{
s = s + ((3 * i + 1.0) / Math.pow(a, 3 * i + 2));
}
return s;
}
}