KnowledgeBoat Logo

Computer Applications

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=x1x2+x3x4+x5... ... ... to n termss = \dfrac{x}{1} - \dfrac{x}{2} + \dfrac{x}{3} - \dfrac{x}{4} + \dfrac{x}{5} …\space …\space …\space to \space n \space terms

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

s=1+(1×2)+(1×2×3)+... ... ... +(1×2×3×4… ... ... ×20)s = 1 + (1 \times 2) + (1 \times 2 \times 3) + …\space …\space …\space + (1 \times 2 \times 3 \times 4 …\space …\space …\space \times 20)

Java

User Defined Methods

ICSE 2016

81 Likes

Answer

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

BlueJ output of 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: (ii) void sumSeries(): to find and display the sum of the following series:BlueJ output of 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: (ii) void sumSeries(): to find and display the sum of the following series:

Answered By

28 Likes