Home / Programs / Design a class to overload a function sumSeries() as follows:
🚀 Programming Example

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

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

📌 Information & Algorithm

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:

\[ \begin{align} s &= \frac{x}{1} - \frac{x}{2} + \frac{x}{3} - \frac{x}{4} + \frac{x}{5} \dots \text{ to } n \text{ terms} \end{align} \]

(ii) void sumSeries(): to find and display the sum of the following series:

\[ \begin{align} s &= 1 + (1 \times 2) + (1 \times 2 \times 3) + \dots + (1 \times 2 \times 3 \times 4 \dots \times 20) \end{align} \]

💻 Program Code

public class RAnsariOverload
{
    void sumSeries(int n, double x) {
        double sum = 0;
        for (int i = 1; i <= n; i++) {
            double t = x / i;
            if (i % 2 == 0)
                sum -= t;
            else
                sum += t;
        }
        System.out.println("Sum = " + sum);
    }
    
    void sumSeries() {
        long sum = 0, term = 1;
        for (int i = 1; i <= 20; i++) {
            term *= i;
            sum += term;
        }
        System.out.println("Sum=" + sum);
    }
}
                        
📚 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.