w3resource
C# Sharp Programming Exercises

C# Sharp Exercises: Calculate the sum of the series [ 1+x+x^2/2!+x^3/3!+....]


C# Sharp For Loop: Exercise-23 with Solution

Write a program in C# Sharp to display the sum of the series [ 1+x+x^2/2!+x^3/3!+....].

Sample Solution:-

C# Sharp Code:

using System;  
public class Exercise23  
{  
    public static void Main()
{
	double x,sum,no_row;
	int i,n;
	
	Console.Write("\n\n");
    Console.Write("Calculate the sum of the series [ 1+x+x^2/2!+x^3/3!+....]:\n");
    Console.Write("------------------------------------------------------------");
    Console.Write("\n\n");  	
	
	Console.Write("Input the value of x :");
    x= Convert.ToInt32(Console.ReadLine());  	
	Console.Write("Input number of terms : ");
	n= Convert.ToInt32(Console.ReadLine());  
	sum =1; no_row = 1;
	for (i=1;i<n;i++)
	{
	  no_row = no_row*x/(float)i;
	  sum =sum+ no_row;
	}
	Console.Write("\nThe sum  is : {0}\nNumber of terms = {1}\nThe value of x = {2}\n",sum,n,x);
  }	
}  

Sample Output:

Calculate the sum of the series [ 1+x+x^2/2!+x^3/3!+....]:                                                                                                     
------------------------------------------------------------                                                                                                   
Input the value of x :5                                                                                                         
Input number of terms : 5                                                                                                         
The sum  is : 65.375                                                                                                         
Number of terms = 5                                                                                                         
The value of x = 5 

Flowchart:

Flowchart: Calculate the sum of the series [ 1+x+x^2/2!+x^3/3!+....]

C# Sharp Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a program in C# Sharp to print the Floyd's Triangle.
Next: Write a program in C# Sharp to find the sum of the series [ x - x^3 + x^5 + ......].

What is the difficulty level of this exercise?



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