C# For 循环计算阶乘

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16583665/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 01:24:18  来源:igfitidea点击:

For loop to calculate factorials

c#asp.netloopsfor-loopfactorial

提问by Anthony Johnson

Currently I have this set of code and its meant to calculate factorials.

目前我有这组代码,它的目的是计算阶乘。

int numberInt = int.Parse(factorialNumberTextBox.Text);

for (int i = 1; i < numberInt; i++)
{
  numberInt = numberInt * i;
}

factorialAnswerTextBox.Text = numberInt.ToString();

For some reason it doesn't work and i have no clue why. For example i will input 3 and get the answer as -458131456 which seems really strange.

由于某种原因,它不起作用,我不知道为什么。例如,我将输入 3 并得到答案为 -458131456,这看起来很奇怪。

Any help appreciated. Thanks

任何帮助表示赞赏。谢谢

采纳答案by Wim Ombelets

int numberInt = int.Parse(factorialNumberTextBox.Text);
int result = numberInt;

for (int i = 1; i < numberInt; i++)
{
    result = result * i;
}

factorialAnswerTextBox.Text = result.ToString();

on a side note: this would normally NOT be the correct way to calculate factorials. You'll need a check on the input before you can begin calculation, in case your starting value is 1 or below, in that case you need to manually return 1.

附带说明:这通常不是计算阶乘的正确方法。在开始计算之前,您需要检查输入,如果您的起始值为 1 或更低,在这种情况下您需要手动返回 1。

On another side note: this is also a perfect example of where recursive methods can be useful.

另一方面:这也是递归方法有用的完美示例。

int Factorial(int i)
{
    if (i <= 1)
        return 1;
    return i * Factorial(i - 1);
}

回答by varun

public static int Factorial(int facno)
{
    int temno = 1;

    for (int i = 1; i <= facno; i++)
    {
        temno = temno * i;
    }

    return temno;
}

回答by Pritesh Tayade

use factorial function:

使用阶乘函数:

static long Factorial(long number)
    {
    if( number <= 1 )
        return 1;
    else
        return number * Factorial(number - 1);
    }

and then call the function:

然后调用函数:

long result = Factorial(int.Parse(factorialNumberTextBox.Text));
factorialAnswerTextBox.Text = result.ToString();

回答by Sathish

 int numberInt=1 ;

            for (int i = 1; i <= int.Parse(factorialNumberTextBox.Text); i++)
            {

                numberInt = numberInt * i;
            }

            factorialNumberTextBox.Text = numberInt.ToString();

回答by Ahmed KRAIEM

You can use this (rather elegant) solution:

您可以使用这个(相当优雅的)解决方案:

    Func<int, int> factorial = null; 
    factorial = x => x <= 1 ? 1 : x * factorial(x-1);
    int numberInt = int.Parse(factorialNumberTextBox.Text);
    factorialAnswerTextBox.Text = factorial(numberInt).ToString();

回答by anupama.kp

Try this,

尝试这个,

int numberInt = int.Parse(textBox1.Text);
        int answer = 1;
        for (int i = 1; i <= numberInt; i++)
        {
            answer = answer * i;
        }

        textBox1.Text = answer.ToString();

回答by ebb

A little late to the party:

晚会有点晚:

Func<int, int> factorial = n => n == 0 ? 1 : 
    Enumerable.Range(1, n).Aggregate((acc, x) => acc * x);

回答by casillas

Two methods are implemented: Recursiveand Basicfactorial calculation.

实现了两种方法:递归基本阶乘计算。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication50
{
    class Program
    {
        static void Main(string[] args)
        {

        NumberManipulator manipulator = new NumberManipulator();
        Console.WriteLine("Please Enter Factorial Number:");
        int a= Convert.ToInt32(Console.ReadLine());

        Console.WriteLine("---Basic Calling--");
        Console.WriteLine("Factorial of {0} is: {1}" ,a, manipulator.factorial(a));

        Console.WriteLine("--Recursively Calling--");
        Console.WriteLine("Factorial of {0} is: {1}", a, manipulator.recursively(a));

        Console.ReadLine();
    }
}

class NumberManipulator
{
    public int factorial(int num)
    {
        int result=1;
        int b = 1;
        do
        {
            result = result * b;
            Console.WriteLine(result);
            b++;
        } while (num >= b);
        return result;
    }

    public int recursively(int num)
    {
        if (num <= 1)
        {
            return 1;
        }
        else
        {
            return recursively(num - 1) * num;
        }
    }
  }
}

回答by KlimentHristov

static void Main()
{
    int numberFactorial = int.Parse(Console.ReadLine());
    int result = numberFactorial;

    for (int i = 1; i < numberFactorial; i++)
    {
        result = result * i;
        Console.WriteLine("{0}*{1}",numberFactorial,i);
    }
    Console.WriteLine(result);
}

回答by hrishikesh

    public static void Main(string[] args)
    {

      string result =   Convert.ToString(GetFactorial(5));
        Console.WriteLine(result);
    }

    internal static int GetFactorial(int factNumber)
    {
        int factorial =1;
        int i = factNumber;            
        while(factNumber>=1)
        {
          factorial = factNumber * factorial;
            factNumber--;
        }
       return  factorial;

    }