Thread: Problem with factorials

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    2

    Problem with factorials

    This is my program. I get no compiling errors but the output is way to high.

    Code:
    // My first factorial program
    #include <iostream>
    using namespace std;
    int main ()
    {
        int start = 0;
        while (start == 0)
        {
        int N;
        int X;
        char choice;
        unsigned long int answer;
        cout << "I am the Factorial program.\n Which number's factorial do you want?\n";
        cin >> N;
        cout << "The factorial of "; cout << N << " is "; cout << answer << endl;
        answer = N;
        X = 1;
        while ( N - X > 1)
        {
              answer = answer * (N - X);
              X = X + 1;
        }
        cout << "This is the end of this program.\n Do you wish to use this program again ( yes (y) or no (n))?\n";
        cin >> choice;
        if (choice == 'y' || choice == 'Y')
        {
        }
        if (choice == 'n' || choice == 'N')
        {
        start = 1;
        }
    }
    
    int end;
    cin >> end;
    return EXIT_SUCCESS;
    
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    You're printing the answer before computing it.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    2
    ok, thank you very much

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 05:46 PM