0

I'm trying to get a factorial to be displayed as for example (factorial of 5 is 5*4*3*2*1)

I'm using a method for the factorial, but it doesn't accept the line Console.Write(i + " x "); in my code.

Any help would be great. here is my code.

//this method asks the user to enter a number and returns the factorial of that number
static double Factorial()
{
    string number_str;
    double factorial = 1;

    Console.WriteLine("Please enter number");
    number_str = Console.ReadLine();

    int num = Convert.ToInt32(number_str);

    // If statement is used so when the user inputs 0, INVALID is outputed
    if (num <= 0)
    {
        Console.WriteLine("You have entered an invalid option");
        Console.WriteLine("Please enter a number");
        number_str = Console.ReadLine();

        num = Convert.ToInt32(number_str);
        //Console.Clear();
        //topmenu();
        //number_str = Console.ReadLine();
    }

    if (num >= 0)
    {
        while (num != 0) 
        {
            for (int i = num; i >= 1; i--)
            {
                factorial = factorial * i;
            }
            Console.Write(i + " x ");

            Console.Clear();
            Console.WriteLine("factorial of " + number_str.ToString() + " is " + factorial);
            factorial = 1;
            Console.WriteLine("(please any key to return to main menu)");
            Console.ReadKey();
            Console.Clear();
            topmenu();
        }
    }

    return factorial;
}

Thank you!

6
  • 2
    i is an integer, you should set it as a string: Console.Write(i.ToString() + " x "); Commented Oct 24, 2013 at 17:39
  • 1
    string.Join("*", Enumerable.Range(1, 5)) is 1*2*3*4*5 Commented Oct 24, 2013 at 17:44
  • @TimS. How is that going to compute the factorial? Commented Oct 24, 2013 at 17:47
  • @ReedCopsey it doesn't, it computes the string. Computing the factorial is another matter, and I don't see much benefit to doing both at the same time: any factorial that fits in a double can be computed in so few iterations (171! > double.MaxValue) that the double looping isn't a performance issue, and it complicates the code. Commented Oct 24, 2013 at 17:49
  • @TimS. True - in that case, to get the same results, you'd need to do string.Join("*", Enumerable.Range(1,5).Reverse())) ;) Commented Oct 24, 2013 at 17:51

2 Answers 2

5

The problem is that your for loop isn't using braces, so the scope is just one line.

Try adding braces appropriately:

for (int i = num; i >= 1; i--)
{
    factorial = factorial * i;
    Console.Write(i.ToString() + " x ");
}

Console.WriteLine("factorial of " + number_str.ToString() + " is " + factorial);    

Without the braces, the i variable only exists on the next statement (factorial = factorial * i;), and no longer exists in scope by the time you call Console.Write.

You will likely also want to remove the call to Console.Clear immediately following this Write, or you will not see it.

Sign up to request clarification or add additional context in comments.

2 Comments

Shouldn't the call to ToString be implicit? Console.Write(1 + "foo"); will compile without issue.
@PrestonGuillot Yeah - being stupid - edited to fix (and show the "real" problem.
1

here's a solution to consider

public static void Main()
{
    Console.WriteLine("Please enter number");

    int input;
    while (!int.TryParse(Console.ReadLine(), out input) || input <= 0)
    {
        Console.WriteLine("You have enter an invald option");
        Console.WriteLine("Please enter number");
    }

    Console.Write("Factorial of " + input + " is : ");

    int output = 1;
    for (int i = input; i > 0; i--)
    {
        Console.Write((i == input) ? i.ToString() : "*" + i);
        output *= i;
    }
    Console.Write(" = " +output);
    Console.ReadLine();
}

int.TryParse() will be beneficial for you, so the program doesn't crash if the user inputs a non-integer

also, you may want something besides an integer. Factorials get very large very fast - anything over 16 will return a wrong result.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.