-2

I need general explanation of the code:

Case1)

In this factorial function, if num is 0, then does it return 0!, which is 1?

Case2) if number is >= than 1, it return fact, which is it's factorial value?

I understand that return 1 and return 0 is both for successful generation of result.

Then why can't I do return 0, in this case?

double factorial(int num)
    {
        int fact = 1;
        int i = 1;
        if (num == 0)
            return 1;
        else
            while (num >= i)
            {
                fact = fact*i;
                i++;
            }
        return fact;
James T
  • 17
  • 1
  • 7
  • You're not returning a status, you're returning a value that the caller can then use. – NathanOliver Oct 17 '17 at 19:43
  • "_Then why can't I do return 0, in this case?_" What? Are you saying that factorial of a number that's not equal to zero is equal to zero? Do you understand how factorial works? In addition, what would be a factorial of a negative number? Since your function accepts those as well. – Algirdas Preidžius Oct 17 '17 at 19:44
  • @AlgirdasPreidžius I'm new to C++ so please don't talk bad to me.. – James T Oct 17 '17 at 19:46
  • 1
    @JamesT How did I "_talk bad_"? I asked questions, yet you, somehow managed to take that as an insult. If you are new, consider reading one of [these C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). SO is not a meant to be used as a tutorial service. – Algirdas Preidžius Oct 17 '17 at 19:48
  • @AlgirdasPreidžius sorry thank you for your advise – James T Oct 17 '17 at 19:51
  • You could search the internet for "C++ factorial (example OR tutorial)". – Thomas Matthews Oct 17 '17 at 19:53

1 Answers1

0
#include <iostream>

using namespace std;

int factorial(int num)              //I changed this to return int since you are taking int and int*int will always be int
    {
        int fact = 1;             
        //int i = 1;                //dont need this
        if (num == 0)
            return fact;            //You can just say return `fact` or `return 1` - i like to make my code readable - s I used `return fact`
                                    //I also prefer to set the value of fact as 1 here and return the 1 at bottom so we only have one return statement
                                    //but thats just me - having 2 return statements should be fine if used wisely

            /*while (num >= i)      //thispart was wrong i reedited it into a better and more efficient code below
            {
                fact = fact*i;
                i++;
            }*/
        else
            {
                while(num>1)        // so lets say we enter 4 - 4 is larger than 1 
                {
                fact*=num;          //first step through it will be fact = fact * num; fact is 1 at first loop so it will be 1 * 4 and we put that value into fact
                num--;              //here we set num to 3 for next loop and we repeat :D
                }
            }

        return fact;                //here we return the value
    }


int main()                          //just a normal main 
{
    int number;
    cout<<"Enter number: \n";
    cin>>number;
    cout<<"Factorial of "<<number<<" is "<<factorial(number);

    return 0;
}

I think your question was perfectly fine and being a beginner programmer myself it also helps me when I see questions like this. Hope this helps! Goodluck!

D.H.
  • 186
  • 14