Thursday, December 8, 2022

Function Overloading ICSE Program On Series

 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)


Program

class Series
{
  public void SumSeries(int n, double x)
 {
    double sum = 0;
    for (int i = 1; i <= n; i++)
 {
      if (i % 2 == 0) 
        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 p = 1;
      for (int j = 1; j <= i; j++) 
{
        p = p * j;
      }
      sum = sum + p;
    }
    System.out.println("Sum = " + sum);
  }
 
    public static void main(String args[])
  {
   Series ob = new Series();
    ob.SumSeries(6,2);
    ob.SumSeries();
  }
}

No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner