Tag Archives: series

Recursive series

ISC Computer Science 2014

Question 9

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

Sum = {x^2/{1!}}+{x^4/{3!}}+{x^6/{5!}}+...{x^n/{(n-1)!}}

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 sum of 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 

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

Specify the class SeriesSum, 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.

The Java implementation of SeriesSum for calculating the recursive series is as follows:

class SeriesSum{
    private int x, n;
    private float sum;
    public SeriesSum(int xx, int nn ){
        x = xx;
        n = nn;
    public double findfact( int m ){
        if( m==0 || m==1 ) return 1;
        else return m * findfact( m - 1 );
    public double findpower( int x, int y ){
        if( y==0 ) return 1;
        else return x * findpower( x, y-1 );
    public void calculate( ){
        for( int i = 1; i<= n; i++ ){
            sum += findpower( x, i*2 ) / findfact( 2 * i - 1 );
    public void display(){
        System.out.println( sum );
    public static void main( String args[] ){
        SeriesSum s = new SeriesSum( 1, 2 );
        s.calculate( );

From the technical standpoint, there is no need to return a double value from the functions, findfact() and findpower, respectively, at line number 8 and 12. Note that neither power nor factorial would have a fractional component as both are accepting integer values. Assuming that it’s being declared double so as to force each term to fractional value, a better option would be to typecast one of the values returned by the function findpower() or findfact() to double or float, at line number 19. Also note that there is a discrepancy in the number of terms ( that is n). It is not correct as per the series given in the question. The last term is xn, since the numerator is starting from 2 and increasing in steps of 2, the number of terms are n/2.