C# to calculate factorial

廉价感情. 提交于 2021-02-10 23:52:42

问题


I have this code that gets an input from the user and calculate its factorial and the factorial for less than the input number, but I keep getting the factorial for the first number only and the rest is 0. It should be like this: For example, if the input is 5:

5! = 120

4! = 24

3! = 6

2! = 4

1! = 1

How do I make the loop go throw all the numbers below the input number?

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

namespace multiple_factorials
{
    class Program
    {
        static void Main(string[] args)
        {
            int num, n;

            Console.WriteLine(".....................[Application 1].....................\n\n");
            Console.WriteLine("Please enter a number to get its factorial: ");
            num = Convert.ToInt32(Console.ReadLine());

            n = num; // Assign n to num

            while (num > 0)
            {
                for (int i = n - 1; i > 0; i--)
                {
                   n *= i;
                }
                Console.WriteLine("Factorial of {0}! = {1}\n", num, n);
                num--;
            }
        }
    }
}

回答1:


You've included System.Linq so I've put a LINQ solution:

 int n = 5;

 // result == 120
 int result = Enumerable.Range(1, n).Aggregate(1, (p, item) => p * item);

However, LINQ is overkill here, and the for loop is more readable. To print out all the lines:

int num = 5;

String result = String.Join(Environment.NewLine,
  Enumerable.Range(1, num)
    .Reverse()
    .Select((index) =>
      String.Format("Factorial of {0}! = {1}\n",
                    index,
                    Enumerable.Range(1, index).Aggregate(1, (p, item) => p * item))));

Console.Write(result);



回答2:


To answer you question now you have acctually posted it:

My question is How to make the loop go throw all the numbers below the input number?

With a loop

for(int i = input; i > 0; i--)

Will count down from input (lets say 5) to 1 {5, 4, 3, 2, 1}

then you simply multiply together

int result = 1;
for(int i = input; i > 0; i--) 
    result *= i; 

Proof: int input = 4; int result = 1; for(int i = input; i > 0; i--) result *= i;

Console.WriteLine("Result=" + result);

Output: 24

A better way Using Recusion

public int Factorial(int f)
{
    if(f == 0)
        return 1;
    else
        return f * Factorial(f-1); 
}

so calling Factorial(5) gives a result of 120 etc.




回答3:


Two things;

Change your int i = n - 1 to int i = num and assign your n to 1 just before your for loop.

while (num > 0)
{
     n = 1;
     for (int i = num; i > 0; i--)
     {                    
           n *= i;
     }           
     Console.WriteLine("Factorial of {0}! = {1}\n", num, n);
     num--;
}

Result will be;

Please Enter A Number To Get Its factorial 5

Factorial of 5! = 120

Factorial of 4! = 24

Factorial of 3! = 6

Factorial of 2! = 2

Factorial of 1! = 1




回答4:


Just move n = num; inside the while loop:

while (num > 0)
{
    n = num;
    for (int i = n - 1; i > 0; i--)
    {
        n *= i;
    }
    Console.WriteLine("Factorial of {0}! = {1}\n", num, n);
    num--;
}



回答5:


As a definition, 0! == 1. There are several ways to deal with this, from the shortest to the most clever, so that you get the sequence: 1, 1, 2, 6, 24, 120... I think that Linq gives the shortest/cleanest code.

int f(i) => Enumerable.Range(1,i<1?1:i).Aggregate((f,x)=>f*x);

Other alternatives, tho less clean, wouold be:

int f(i) => Enumerable.Range(1,Math.Max(1,i).Aggregate((f,x)=>f*x);

int f(i) => Enumerable.Range(1,i+(int)Math.Pow(0,i)).Aggregate((f,x)=>f*x);


来源:https://stackoverflow.com/questions/29027000/c-sharp-to-calculate-factorial

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!