Thread: calculate the factorial and display in two column.

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

    Red face calculate the factorial and display in two column.

    1. Calculate the factorial for values 1-20 and display the results Display the results in two columns. Left column 1 – 10 and the right column 11 – 20.
      Here is my code, can someone help me.

      Code:
      void FactorialNumberFor1To20(int intIndex)
      {
          int intFactorial = 0;
          int intArraySize[19];
      
              intFactorial = intArraySize[19];
              for (intIndex = intArraySize[19]; intIndex > 1; intIndex -= 1)
              {
                  intFactorial = intFactorial * (intIndex - 1);
                  intArraySize[intIndex] = intFactorial;
              }
      
              cout << intArraySize[intIndex] << setw(40) << " " << intArraySize[intIndex + 10];
      }

  2. #2
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    I might suggest starting simpler and writing a function that calculates the factorial for a given integer.
    Code:
    int factorial(int const x)
    {
     // implement
    }
    Then you can begin composing the rest of your programming logic in terms of this function.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Consider also, the fact that 20! is far too big for an int, on most platforms.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  4. #4
    Registered User
    Join Date
    Oct 2016
    Posts
    2
    I don't have any idea about how to display the result in two columns after I calculated the factorial.

  5. #5
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Quote Originally Posted by Elkvis View Post
    Consider also, the fact that 20! is far too big for an int, on most platforms.
    Oh shoot! It looks like it just barely fits in a long int or whatever the 8 byte int type is. Man, it's pretty easy to see how large factorials can become!

    This might also be a good lesson in how and why to use typedefs for this kind of thing.

    But as for displaying, it might be easiest to just store all 20 factorial calculations in an array. Then you can simply iterate it and print as you go. Including the weird `std::cout << arr[0] << " " << arr[10] << "\n";` to get 2 columns.
    Last edited by MutantJohn; 10-27-2016 at 12:28 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 12-07-2013, 03:51 PM
  2. Replies: 3
    Last Post: 12-06-2013, 07:15 PM
  3. Replies: 13
    Last Post: 05-02-2012, 04:17 PM
  4. calculate column-wise sum of a matrix in C
    By Raymond2010 in forum C Programming
    Replies: 4
    Last Post: 06-21-2011, 11:21 PM
  5. Tab control: how to calculate a tab display area???
    By SundayDeveloper in forum Windows Programming
    Replies: 4
    Last Post: 09-06-2007, 03:19 PM

Tags for this Thread