Home Java Programming Language / Programs / Design a class to overload a function series( ) as follows: double series(double n) with one double argument and returns the sum of the series.sum = (1/1) + (1/2) + (1/3) + .......... + (1/n) 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
🚀 Programming Example

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

  1. double series(double n) with one double argument and returns the sum of the series.
    sum = (1/1) + (1/2) + (1/3) + .......... + (1/n)
  2. 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

👁 107 Views
💻 Practical Program
📘 Step Learning
Learn this program step-by-step with algorithm, source code, output and detailed explanation.
No previous program
No next program

💻 Program Code

public class RAnsariSeries
{
    double series(double n) {
        double sum = 0;
        for (int i = 1; i <= n; i++) {
            double term = 1.0 / i;
            sum += term;
        }
        return sum;
    }

    double series(double a, double n) {
        double sum = 0;
        int x = 1;
        for (int i = 1; i <= n; i++) {
            int e = x + 1;
            double term = x / Math.pow(a, e);
            sum += term;
            x += 3;
        }
        return sum;
    }

    public static void main(String args[]) {
        RAnsariSeries obj = new RAnsariSeries();
        System.out.println("First series sum = " + obj.series(5));
        System.out.println("Second series sum = " + obj.series(3, 8));
    }
}
                        

🖥 Program Output

First series sum = 2.283333333333333
Second series sum = 0.1286982248418103
Press any key to continue . . .

                            
No previous program
No next program
📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.

🔥 Practice suggestion

Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.