پروژه فاکتوریل با c#
نویسنده :
1!+2!+3!+4!.....
1!+2!+3!+4!.....
و
1/1!+1/2!+1/3!+1/4!....
int n = 10;
StringBuilder builder = new StringBuilder();
double resultValue = 0;
double temp = 1;
for (int i = 0; i < n; i++)
{
if (i > 0)
builder.Append(" + ");
builder.Append(string.Format("{0}!", i + 1));
temp *= (i + 1);
resultValue += temp;
}
//
builder.Append(string.Format(" = {0}", resultValue));
_textBox.Text = builder.ToString();
int n = 10;
StringBuilder builder = new StringBuilder();
double resultValue = 0;
double temp = 1;
for (int i = 0; i < n; i++)
{
if (i > 0)
builder.Append(" + ");
builder.Append(string.Format("1/{0}!", i + 1));
temp *= (i + 1);
resultValue += 1/temp;
}
//
builder.Append(string.Format(" = {0}", Math.Round(resultValue, 2)));
_textBox.Text = builder.ToString();
--------------------------------------------
//The fucntion can be called in page load or in button click or as you like.
protected void Page_Load(object sender, EventArgs e)
{
// Call your fucntion here and pass your input to this method
String Factorials = YourFactorial(3);
}
public String YourFactorial(int n)
{
if (n <= 1)
{
return " 1! :" + " The result is : 1" ;
}
int result = 1;
String results = "";
results = "1! + ";
for (int i = 2; i <= n; i++)
{
results =results + i.ToString()+" ! + ";
result = result * i;
}
return results + " The result is :" + result;
}