使用循环计算C#中的阶乘

时间:2013-03-04 00:25:12

标签: c# math loops for-loop

这是我到目前为止所做的:

namespace factorials
{
    class Program
    {
        static void Main(string[] args)
        {
            int number;

            do
            {
                Console.WriteLine("What non-negative integer do you want to factorial?");
                while (!int.TryParse(Console.ReadLine(), out number))
                    Console.WriteLine("Please enter a whole number only");
                calculate(ref number);
            } while (number >= 0);
            Console.WriteLine("Please enter a non-negative number");

        }
        static void calculate(ref int number)
        {
            int factorial;
            int counter;

            for (counter = number; counter <= number; counter++)
            {
                factorial = number * number;
                Console.WriteLine("The factorial of {0} is {1}", number, factorial);
            }

    }
    }
    }

现在它只是给了我数字的平方,而不是它们的阶乘。如何使其作为输入重复次数,从而产生因子?

此外,我不确定是否有必要将程序限制为非负整数,但如果我想要那个部分只是在那里结束程序而不是循环回到开头。

8 个答案:

答案 0 :(得分:6)

for循环根本没有任何意义!如果您正在寻找阶乘,那么您必须将所有数字从一个乘以给定数字。那将是:

int factorial = 1;

for (counter = 1; counter <= number; counter++)
{
    factorial = factorial * counter;

}

Console.WriteLine("The factorial of {0} is {1}", number, factorial);

这将计算一个数的阶乘。你必须为你想要的所有数字重复它。

答案 1 :(得分:0)

你的循环将数字的平方分配给循环中的结果,然后立即退出。您需要对其进行更改,以便将结果重复乘以1到N之间的数字。

将1分配给阶乘,并将其乘以循环中的计数器:

factorial *= counter;

别忘了在1点开始你的计数器。

答案 2 :(得分:0)

我会给你提示。

  1. 将变量初始化为1.我们将其称为Answer
  2. 运行从1到Number的循环,执行Answer = Answer * loopIterationVariable的操作。在每次迭代之后,将循环迭代变量递增1。

答案 3 :(得分:0)

static void Main(string[] args)
    {

        Console.WriteLine("Enter an integer number to factorise");

        int ans = 1;
        int a = int.Parse(Console.ReadLine());

        for (int i = 1; i <= a; i++)
        {
            ans = ans * i;
        }
        Console.WriteLine(ans.ToString());
        Console.ReadLine();             
    }

答案 4 :(得分:0)

        string str = Interaction.InputBox("Enter the number to find the factorial of: ");
        double counter = double.Parse(str);
        long factorial = 1;

        while (counter > 1)
        {
            factorial *= (long)counter--;//factorial = factorial * counter - 1
        }
        MessageBox.Show("The factorial of " + str + " is " + String.Format("{0:N}", factorial));

我使用了Microsoft.VisualBasic方法的Interaction.InputBox()引用

答案 5 :(得分:0)

递归实现以及基本实现如下

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

答案 6 :(得分:0)

    private void btnCalculate_Click(object sender, EventArgs e)
    {
        try
        {
            userinputF = Int32.Parse(txtFaculteit.Text);
            for (counter = 1; counter <= userinputF; counter++)
            {
                answer = answer *=  counter;
            }          
        }
        catch (Exception exception)
        {

                MessageBox.Show("Please fill in a number " + exception.Message);
        }

        lblAnswerFaculteit.Text = lblAnswerFaculteit.Text + "The faculty of " + userinputF + " = " + answer;
    }
}

}

答案 7 :(得分:0)

    int userinputF;
    int counter;
    int answer =1;


    public Form1()

    {
        InitializeComponent();

    }

    private void btnCalculate_Click(object sender, EventArgs e)
    {
        try
        {
            userinputF = Int32.Parse(txtFaculteit.Text);
            for (counter = 1; counter <= userinputF; counter++)
            {
                answer = answer *=  counter;
            }          
        }
        catch (Exception exception)
        {

                MessageBox.Show("Please fill in a number " + exception.Message);
        }

        lblAnswerFaculteit.Text = lblAnswerFaculteit.Text + "The faculty of " + userinputF + " = " + answer;
    }
}

}