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:
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 - x^7 + x^9 -.....].
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework