0

I am writing a program to find the factorial of a user inputted number. My program works from, except for finding the factorial of 0. The requirement is that the factorial of 0 should output one, but I cannot think of a way to write this capability into the code without creating a special case for when 0 is entered. This is what I have so far

#include <iostream>
#include <cmath>
using namespace std;

int main() {

    int startingNumber = 0;
    double factorialize = NULL;

    while(startingNumber != -1) {
        cout << "Enter the numbr to factorial: ";
        cin >> startingNumber;
        factorialize = startingNumber;
        for(int x=startingNumber-1;x>=1;x--) {
            factorialize = factorialize*x;
        }
        cout << factorialize << endl;
        factorialize = NULL;
    }

    return 0;
}

This outputs a factorial accurately for all cases except 0. Is there a way to do this that doesn't require a special case? I am thinking no because when I read about the reasons for why 0! is 1, it says that it is defined that way, in other words, you cannot reason your way into why it is 1. Just like x^0, 0! = 1 has a different logic as to why than why 2^2 is 4 or 2! = 2.

comu
  • 921
  • 1
  • 11
  • 29
  • 3
    Are you familiar with recursion? Solving a factorial is a very good candidate for a [recursive solution](http://stackoverflow.com/questions/18090465/recursion-in-c-factorial-program) – Cory Kramer Oct 14 '14 at 19:41
  • 3
    well then, create a special case for when 0 is entered. – Sam I am says Reinstate Monica Oct 14 '14 at 19:41
  • 1
    Where's the problem? You have a special case in your problem domain, so why should the code not reflect that? Of course, what you should do is to separate the calculation from any I/O. – Christian Hackl Oct 14 '14 at 19:44
  • 2
    By the way, `NULL` is a way to express null pointers (which was never really necessary because you could also just write `0`, and which has become completely obsolete with C++11's `nullptr` keyword). Using it for anything but a pointer only works in a very indirect way. For `double`, use `0.0`. – Christian Hackl Oct 14 '14 at 19:46
  • the whole point of the question is if there is a way to not make a special case – comu Oct 14 '14 at 19:46
  • 1
    @comu: The special case is already there. You cannot make it disappear, you can only obfuscate it IMO. – Christian Hackl Oct 14 '14 at 19:47
  • 1
    "This outputs a factorial accurately for all cases" Really? 2! = 4? 3! = 18? 4! = 96? Perhaps you should refresh your understanding of the factorial function. – molbdnilo Oct 14 '14 at 20:02
  • copying error, fixed that, now its accurate. However @Lashane has a working correct answer – comu Oct 14 '14 at 20:11
  • The Factorial is defined to have the special case of 0! = 1 for convenience. There are gimmicks you can use to avoid having an explicit special case for 0!, but why do this when it just makes the logic more obscure unnecessarily? – Swiss Oct 14 '14 at 20:31
  • A "special case" called a *termination case* is **how recursive functions are defined.** After all, if you don't stop recursion somewhere you have infinite recursion. – cdhowie Oct 14 '14 at 20:46

5 Answers5

4

try this:

    factorialize = 1;
    for(int x=2; x<=startingNumber;x++)
        factorialize *= x;
Iłya Bursov
  • 23,342
  • 4
  • 33
  • 57
3

Try this:

for (unsigned int n; std::cin >> n; )
{
    unsigned int result = 1;
    for (unsigned int i = 1; i <= n; ++i) { result *= i; }
    std::cout << n << "! = " << result << "\n";
}

You can change the result type a bit (unsigned long long int or double or long double), but ultimately you won't be able to compute a large number of factorials in hardware.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
0

First of all I do not see how it can be calculated accurately, as you multiply startingNumber twice. So just fix the logic with:

factorialize = 1.0;
for(int x=startingNumber;x>=1;x--) {
    factorialize = factorialize*x;
}

And it should calculate factorial properly as well as handling 0 the proper way. Also you should not use NULL as initial value for double, it is for pointers.

Slava
  • 43,454
  • 1
  • 47
  • 90
0

There is a complete factorial of number program of C++ which includes the facility of factorial of positive number,negative and zero.

    #include<iostream>
    using namespace std;
    int main()
    {
        int number,factorial=1;
        cout<<"Enter Number to find its Factorial: ";
        cin>>number;
        if(number<0

)
    {
        cout<<"Not Defined.";
    }
    else if (number==0)
    {
        cout<<"The Facorial of 0 is 1.";
    }
    else
    {
      for(int i=1;i<=number;i++)
      {
          factorial=factorial*i;
      }
    cout<<"The Facorial of "<<number<<" is "<<factorial<<endl;
    }
    return 0;
}

You can read full program logic on http://www.cppbeginner.com/numbers/how-to-find-factorial-of-number-in-cpp/

0

The function listed below returns the factorial FASTER than any solution posted here to this date:

const unsigned int factorial(const unsigned int n) 
{
    unsigned int const f[13] = { 1,1,2,6,24,120,720,5040,40320,362880,3628800,39916800,479001600 };
    return f[n];
}

I looks silly but it works for all factorials that fit into a 32-bit unsigned integer.

George Robinson
  • 1,500
  • 9
  • 21