Search code examples
c#recursionfactorial

Recursion factorial code c# - "Invalid token" error


I am attempting to write a recursion code that computes the factorial from a given number. (Factorial of 3 is "3*2*1 = 6"). I have written the following code, and the following error message is printed

"Invalid token '(' in class, struct, or interface member declaration"

I have checked my code and in my eyes, I can't see an error, is there anything I can do to fix the issue? The c# code is posted below. (ps. I'm not a c# wizz.)

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace Rextester
{
    public class Program
    {
        public static void Main(string[] args)
        {
            int num1;
            int num2;
            int num3 = 0;

            Console.WriteLine("Insert number");
            num1 = Console.ReadLine();

            num2 = num1 -1;

            factorial(num1, num2, num3);

            Console.WriteLine("Your number is {0}", factorial());
        }
        Console.ReadKey();


        static int factorial(int a, int b, int c)
        {
            if (a > 0)
            {
                a * b = c;
                factorial(a - 1, c, c);
                return c;
            }
        }
    }

}

Solution

  • There are a number of problems with your code, and it does not compile, lets go through each error

    Invalid token '(' in class, struct, or interface member declaration

    This, as pointed out in the other answers is the Console.Readkey() in the middle of your class outside of any method. Move it into the bottom of the Main method.

    Cannot implicitly convert type 'string' to 'int'

    This is due to the line num1 = Console.ReadLine(); as that method returns a string and you're trying to assign it to an int. The right way to deal with this is to check the user input, to ensure they have typed a number. For brevity I'll do it the wrong and just assume its right.

    num1 = int.Parse(Console.ReadLine()); // Use TryParse here, and notify the user if they typed something wrong
    

    No overload for method 'factorial' takes 0 arguments

    This is because you've tried to call your factorial method with no arguments. I think you were trying to output the result from the call above.

    var result = factorial(num1, num2, num3);
    Console.WriteLine("Your number is {0}", result);
    

    The type or namespace name 'a' could not be found (are you missing a using directive or an assembly reference?)

    This is due to this line: a * b = c; which just doesn't make sense. I assume you meant c = a * b;

    Finally

    'Rextester.Program.factorial(int, int, int)': not all code paths return a value

    You need to return a value outside the if of factorial. Here you then get into an endless loop, but at least your code compiles! Now just fix your algorithm. This is much simpler

    static int factorial(int n)
    {
        if(n == 1)
            return 1;
    
        return factorial(n-1) * n;
    }
    

    Live example: http://rextester.com/OUCC98161