如何在C#中打印阶乘系列? [英] How to print factorial series in C#?

查看:83
本文介绍了如何在C#中打印阶乘系列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在C#中打印以下阶乘系列...

1!+2!+3!+4!.....



1/1!+1/2!+1/3!+1/4!....



Plz帮忙..

How to print following factorial series in C#...
1!+2!+3!+4!.....
and
1/1!+1/2!+1/3!+1/4!....

Plz help out..

推荐答案

编写一个计算给定数字的阶乘的方法。这是最重要的事情。下一步是找到给定表达式的值。如果只计算4次阶乘,请调用该方法4次。如果它是某种系列,写另一种方法,你将传递'n',其中n是计算阶乘的数字。在这个方法中,你将使用循环调用你编写n次的第一个方法(while / for)。

你必须先BEGIN。
Write a method that calculates factorial of a given number. That's the most important thing. Next step is to find the value of a given expression. If it's just calculating the factorial 4 times, call the method 4 times. If it's some kind of series, write another method where you will pass 'n' where n is the number till which the factorial will be calculated. In this method you will call the first method you wrote n times using a loop (while / for).
You gotta BEGIN first.


1.For first你应该在下一个例子中做的表达:

1.For first expression you should do like in the next example:
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();





2.对于第二个表达式,类似的代码如下:



2.For 2nd expression a similar code like below:

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();


检查此代码希望这对您有所帮助。



Check this code hope this will help you.

//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;

    }


这篇关于如何在C#中打印阶乘系列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆