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

本文关键字:阶乘 计算 循环 | 更新日期: 2023-09-27 18:02:06

这就是我目前所拥有的:

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

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

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

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

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

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

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

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

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

factorial *= counter;

别忘了1点开始你的柜台。

我会给你提示。

  1. 将变量初始化为1。让我们把它说成Answer
  2. 运行从1到Number的循环,执行Answer = Answer * loopIterationVariable的操作。每次迭代后,将循环迭代变量递增1
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();             
    }
        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()方法

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

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

}

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

}