Series Sum ISC Computer Science 2014 Theory

A class SeriesSum is designed to calculate the sum of the following series:

Some of the members of the class are given below:

Class name: SeriesSum

Data members/instance variables:
x: to store an integer number
n: to store number of terms
sum: double variable to store the sum of the series

Member functions:
SeriesSum(int xx, int nn): constructor to assign x = xx and n = nn
double findFact(int m): to return the factorial of m using recursive technique
double findPower(int x, int y): to return x raised to the power of y using recursive technique
void calculate(): to calculate the sum of the series by invoking the recursive functions respectively
void display(): to display the sum of the series

(a) Specify the class SumSeries, giving details of the constructor(int, int), double findFact(int), double findPower(int, int), void calculate() and void display(). Define the main() function to create an object and call the functions accordingly to enable the task.

import java.util.Scanner;
class SeriesSum{
    int x;
    int n;
    double sum;
    public SeriesSum(int xx, int nn){
        x = xx;
        n = nn;
        sum = 0.0;
    }
    public double findFact(int m){
        if(m == 0 || m == 1)
            return 1.0;
        return m * findFact(m - 1);
    }
    public double findPower(int x, int y){
        if(y == 0)
            return 1.0;
        return x * findPower(x, y - 1);
    }
    public void calculate(){
        for(int i = 2; i <= n; i += 2)
            sum += findPower(x, i) / findFact(i - 1);
    }
    public void display(){
        System.out.println("Series sum: " + sum);
    }
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("x = ");
        int xx = Integer.parseInt(in.nextLine());
        System.out.print("n = ");
        int nn = Integer.parseInt(in.nextLine());
        SeriesSum obj = new SeriesSum(xx, nn);
        obj.calculate();
        obj.display();
    }
}

(b) State the two differences between iteration and recursion.

Iteration does not depend on stack, whereas recursion depends on stack. In each call, separate variables are created during recursion, whereas in iteration, same variables are used.

Comments

Popular posts from this blog

Encrypt Program ISC Specimen 2023 Theory

No Repeat Program ISC Computer Science 2022 Semester 2 Theory

Bank Inheritance Program ISC Specimen 2023 Theory