Wednesday, December 14, 2022

Function Overloading Series Function For ICSE

 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 / a^2  +  4 / a^5  +  7 / a^8  +  10 / a^11 + … to n terms.


Program


class Ser
{
    public double series(double n)
{
        double sum = 0.0;
        for(int i = 1; i <= n; i++)
            sum = sum + 1.0 / i;
        return sum;
    }
    public double series(double a, double n)
{
        double sum = 0.0;
        int j = 1;        
        for(int i = 1; i <= n; i++)
{
            sum =sum + j/ Math.pow(a, j+1);
           j=j+3;
        }
        return sum;
    }
  public static void main(String args[])
  {
    Ser ob = new Ser();
    System.out.println(ob.series(5.0));
    System.out.println(ob.series(1.3,6.0)); 
  }
}


No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner