icse-promo

Question

A class SeriesSum is designed to calculate the sum of the following series:
Sum=(x2 / 1!)+(x4 / 3!)+(x6 / 5!)+….(xn /(n- 1)!)
Some of the members of the class are given below:
Classname:SeriesSum
Data members / instance variables:
x:to store an integer number
n:to store number of terms
sum: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
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.

Share code with your friends

Share on whatsapp
Share on facebook
Share on twitter
Share on telegram

Code

				
					import java.util.Scanner;
public class SeriesSum
{
    int x,n;
    double sum;
    SeriesSum(int xx,int nn)
    {		
        x=xx;
        n=nn;
        sum=0.0;
    }
    
    double findfact(int a)
    { 
        if(a==1)
        {
            return 1;
        }
        else
        {
            return a* findfact(a-1);
        }

    }
    
    double findpower(int a,int b)
    { 
        if(b==0)
        {
            return 1;
        }
        else
        {
            return a* findpower(a,b-1);
        }

    }
    
    void calculate()
    { 
        int k=2;
        for(int i=1;i<=n;i++)
        {
            sum+=findpower(x,k)/findfact(k-1);
            k=k+2;
        }
    }
    
    void display()
    {
        System.out.println("sum="+sum);
    }
    
    public static void main()
    {
        Scanner sc=new Scanner(System.in);
        System.out.println("ENTER A NUMBER");
        int a=sc.nextInt();
        System.out.println("ENTER  NUMBER OF TERMS");
        int b=sc.nextInt();
        SeriesSum obj1=new SeriesSum(a,b);
        obj1.calculate();
        obj1.display();
    }
}




				
			

Coding Store

Leave a Reply

Your email address will not be published. Required fields are marked *