Tuesday, December 6, 2022

Function Overloading Programs on Series

 Design a class to overload a function series() as follows:


(a) void series(int x, int n): to display the sum of the series given below:

x1 + x2 + x3 + … + xn terms


(b) void series(int p): to display the following series:

0, 7, 26, 63, … p terms.


(c) void series(): to display the sum of the series given below:

1/2 + 1/3 + 1/4 + … + 1/10.


Program on Function  Overloading To Display Series

public class Series
{
 
    public void series(int x, int n)
    {
        double sum = 0L;
        for(int i = 1; i <= n; i++)
        {
            sum=sum+Math.pow(x, i);
        }
        System.out.println("Sum of series is (1st Overloaded function)" + sum);
    }
    public void series(int p)
    {
        int term=0;
        System.out.print("\n2nd Overloaded function ");
        for(int i = 1; i <= p; i++)
        {
            term = (int)(Math.pow(i, 3)-1);
            System.out.print(term + " ");
        }
    }
    public void series()
    {
        double sum = 0.0;
        for(int i = 2; i <= 10; i++)
            sum = sum+ 1.0 / i;
        System.out.println("\n\nSum of series is (3rd Overloaded function)" + sum);
    }
    
    public static void main(String args[])
    {
      Series ob = new Series();
      ob.series(2,4);
      ob.series(6);
      ob.series();  
    } 
  
}

Back To BlueJ Series Display Programs Home Page: CLICK HERE

No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner