1

fairly new so please forgive the question. I've written a method to take a string, covert it to a number, and return a Factorial of that number. That works fine. Its calling the method and printing the result that is confusing me. Here is the code:

static void Main(string[] args)
{
    Console.WriteLine("Type number to do factorial on..");
    var calc = Fact(Console.ReadLine());
    Console.WriteLine("The answer is " + calc);
}

private static string Fact(string numFact)
{
    var number = 1;
    int factorial = Convert.ToInt32(numFact);
    for (int i = 1; i<= factorial; i++)
    {
        number *= i;
    }
   // Console.WriteLine(number); added to test it works
    return numFact;
}

Can somebody help please? As you'll probably guess from looking, If I input 5, I get 5 returned.

5
  • 7
    You are returning the wrong value. You want to return number; Also, change the function signature to private static int Fact(string numFact) Commented May 17, 2018 at 18:35
  • You are returning the same numFact number from the method which you are passing to it. Commented May 17, 2018 at 18:37
  • @JohnnyMopp - instead of changing signature of the function, why not just return number.ToString() Commented May 17, 2018 at 18:41
  • @Fabio Well, since it's a math function, returning a number seems natural. But either way works. Commented May 17, 2018 at 18:42
  • and going on with @JohnnyMopp, the function should take an int as parameter, leaving the caller the conversion from string Commented May 17, 2018 at 19:11

2 Answers 2

3
static void Main(string[] args)
{
    Console.WriteLine("Type number to do factorial on..");
    var calc = Fact(Console.ReadLine());
    Console.WriteLine("The answer is " + calc);
}

private static int Fact(string numFact)
{
    var number = 1;
    int factorial = Convert.ToInt32(numFact);
    for (int i = 1; i<= factorial; i++)
    {
        number *= i;
    }
   // Console.WriteLine(number); added to test it works
    return number;
}

As the comments mention,

  • You are returning numFact instead of number.
  • You should make it return an int instead of string for a better practice, though this isn't exactly necessary, it just feels more natural since it is a number. Make it a string once you need it as a string, doing so early could cause problems later; however, since this is a very small project the likelihood of that is small so do what you like.
Sign up to request clarification or add additional context in comments.

3 Comments

Ive edited your answer as you need to send it back ToString() because var number = 1 is an int the way the OP has written it. BTW thats not me thats marked you down on that one..
@SimonPrice I rejected it because you fail to notice that I switched the return type to int and not string the Main function should be fine since the OP uses var and integers concatenate with strings implicitly call ToString().
Thank you all for your responses. Much appreciated. It did actually click before I came and checked, that I was returning the wrong value type! Though I've picked up some other hints so this page isn't wasted! Thanks again :)
0

You should dig a bit deeper with your logging. You logged only 1 of 2 critical variables and so you didn't get the answers you needed from the logs. Also, take the Console.Readline() out of the equation. Use a hard coded number. Eventually you can write tests. Checkout Test Driven Development (TDD) which is perfect for this exercise.

Just modifying the logs without TDD can bring out something like this:

static void Main(string[] args)
{
    //Console.WriteLine("Type number to do factorial on..");
    int sampleNumber = 5;
    string sampleNumberAsString = sampleNumber.ToString();
    var calc = Fact(sampleNumberAsString);
    Console.WriteLine("Outside fact the answer is " + calc);
}


private static int Fact(string numFact)
{
    var number = 1;
    int factorial = Convert.ToInt32(numFact);
    for (int i = 1; i <= factorial; i++)
    {
        number *= i;
    }
    Console.WriteLine($"Inside Fact() local variable number is {number}");//Inside Fact() local variable number is 120
    Console.WriteLine($"Inside Fact() numFact parameter is is {numFact}");//Inside Fact() numFact parameter is is 5
    //return numFact;//Clearly I want to return the calculated value (number) not the original value passed in (numFact)
    //I'm calculating a number so let's not worry about what the caller does with the number
    //Change the return type to int.
    return number;
}

1 Comment

Hi, thanks for the advice. Your right of course, though this is just for an exercise for myself, so not really necessary. Though ill take this under advisement!

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.