w3resource
C# Sharp Programming Exercises

C# Sharp Exercises: Calculate the sum of the series [ 1-X^2/2!+X^4/4!- .........]

C# Sharp For Loop: Exercise-18 with Solution

Write a program in C# Sharp to find the sum of the series [ 1-X^2/2!+X^4/4!- .........].

Sample Solution:

C# Sharp Code:

using System;  
public class Exercise18 
{  
    public static void Main()
{
	double x,sum,t,d;
	int i,n;
	
	Console.Write("\n\n");
    Console.Write("Calculate the sum of the series [ 1-X^2/2!+X^4/4!- .........]:\n");
    Console.Write("----------------------------------------------------------------");
    Console.Write("\n\n");	
	
	Console.Write("Input the Value of x :");

    x= Convert.ToInt32(Console.ReadLine()); 	
	Console.Write("Input the number of terms : ");
    n= Convert.ToInt32(Console.ReadLine()); 	
	sum =1; t = 1;
	for (i=1;i<n;i++)
	{
	  d = (2*i)*(2*i-1);
	  t = -t*x*x/d;
	  sum =sum+ t;
	}
	Console.Write("\nthe sum = {0}\nNumber of terms = {1}\nvalue of x = {2}\n",sum,n,x);
 } 
} 

Sample Output:

Calculate the sum of the series [ 1-X^2/2!+X^4/4!- .........]:                                                                                                 
----------------------------------------------------------------    
Input the Value of x :5                                                                                                                                        
Input the number of terms : 2                                                                                                         
the sum = -11.5                                                                                                         
Number of terms = 2  
value of x = 5  

Flowchart:

Flowchart: Calculate the sum of the series 1-X^2/2+X^4/4- .

C# Sharp Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a program in C# Sharp to make such a pattern like a pyramid with number which will repeat the number in the same row.
Next: Write a program in C# Sharp to calculate the harmonic series and their sum.

What is the difficulty level of this exercise?



New Content: Composer: Dependency manager for PHP, R Programming