C#

Очень прошу помощи в написании программы на си шарп :)

Разработать алгоритм и программу для вычисления суммы с заданным числом членов ряда (цикл - for )
using System;
namespace Answer {
class Program {
static private void Main() {
Console.Write("Количество членов ряда: ");
var n = int.Parse(Console.ReadLine());
var s = Sum(n);
Console.WriteLine($"Сумма ряда: {s}");
Console.ReadKey();
}
static public double Factorial(int n, double m = 1) {
if (n == 1) return m;
m *= 2 * n;
return Factorial(n - 1, m);
}
static public double Sum(int n, double s = 0) {
if (n == 0) return s;
s += 1 / Factorial(n);
return Sum(n - 1, s);
}
}
}
Андрей Назарян
Андрей Назарян
96 632
Лучший ответ
using System;

public class Task
{

private int n;

/**
* int n - число членов ряда
*/
public Task(int n)
{
this.n = n;
}
/**
* int n - число для которого считаем факториал
*/
public int factorial(int n)
{
int r = 1;
for (int i = 1; i <= n; i++) {
r = r * i;
}

return r;
}

/**
* Здесь просто считаем сумму членов ряда
*/
public double calculate()
{
double r = 1.0;
int p = 2;
for (int i = 1; i <= this.n; i++) {;
r += (1.0 / this.factorial(p) );
p += 2;
}

return r;
}
}

class TestTask
{
static void Main()
{
Console.WriteLine("Enter the quantity items");
string input = Console.ReadLine();

int n = 0;
try {
n = Convert.ToInt32(input);
} catch (OverflowException) {
Console.WriteLine("{0} is outside the range of the Int32 type.", input);
return;
} catch (FormatException) {
Console.WriteLine("The {0} value '{1}' is not in a recognizable format.",
input.GetType().Name, input);
return;
}

var task = new Task(n);
Console.Write("Sum = ");
Console.Write(task.calculate());
Console.WriteLine("");

Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
}