CodeHub Menu
Home Exercise Practice About Download Portfolio Play Games NEW Privacy Policy Terms Contact
Login / Register

overload a function sumSeries in JAVA

JAVA November 21, 2025 4 views 0 likes
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 = x1 x2 + x3 x4 + x5 − … to n terms



(ii) void sumSeries(): to find and display the sum of the following series:
S=1+(1×2)+(1×2×3)+⋯+(1×2×3×…×20)

Answer:



Overload - Java Code

Overload - Java Code


public class KboatOverload
{
    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);
    }
}


OUTPUT:


Overloaded Series Calculator

Overload

1. Alternating Series: x/1 - x/2 + x/3 - ...

2. Sum of factorial series: 1! + 2! + ... + 20!

4 views 0 likes
Total visits: 2,089 • Unique visitors: 1,285