user2916941
user2916941

Reputation: 25

C# Console Output Formatting

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!

Upvotes: 0

Views: 1062

Answers (2)

Jonesopolis
Jonesopolis

Reputation: 25370

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.

Upvotes: 1

Reed Copsey
Reed Copsey

Reputation: 564851

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.

Upvotes: 5

Related Questions