Print factorials of a range in right aligned format

Given two number m and n, the task is to find factorial of all number including m and n and then print below format.

Examples:

Input : 6 10
Output :
     720
    5040
   40320
  362880
 3628800

Input : 10 20
Output :
             3628800
            39916800
           479001600
          6227020800
         87178291200
       1307674368000
      20922789888000
     355687428096000
    6402373705728000
  121645100408832000
 2432902008176640000


We used the boost multiprecision library for store the factorial of large number and print the factorial with the setw() function.
setw(int) -> setw(int) is a function is used for intention in the result.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP Program to print format of factorial
#include <boost/multiprecision/cpp_int.hpp>
#include <iostream>
#include <vector>
using namespace std;
using boost::multiprecision::cpp_int;
  
vector<cpp_int> find_factorial(int num1, int num2)
{
    // vector for store the result
    vector<cpp_int> vec;
  
    // variable for store the
    // each number factorial
    cpp_int fac = 1;
  
    // copy of first number
    int temp = num1;
  
    // found first number factorial
    while (1) {
        if (temp == 1)
            break;
        fac *= temp;
        temp--;
    }
      
    // push the first number in result vector
    vec.push_back(fac);
  
    // incerement the first number
    num1++;
  
    // found the all reaming number 
    // factorial loop is working until
    // all required number factorial founded
    while (num1 <= num2) {
        fac *= num1;
  
        // store the result of factorial
        vec.push_back(fac);
  
        // incerement the first number
        num1++;
    }
      
    // return the result
    return (vec);
}
  
// function for print the result
void print_format(vector<cpp_int>& result)
{
    // setw() is used for fill the blank
    // right is used for right justification of data
    int digits = result.back().str().size();
      
    for (int i = 0; i < result.size(); i++) {
         cout <<  setw(digits + 1) <<  
                    right << result[i] <<  endl;
    }
}
  
// Driver function
int main()
{
      
    // number which found the factorial
    // of between range
    int m = 10, n = 20;
  
    // store the result of factorial
    vector<cpp_int> result_fac;
  
    // function for found factorial
    result_fac = find_factorial(m, n);
  
    // function for print format
    print_format(result_fac);
  
    return 0;
}

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?PHP
// PHP Program to print 
// format of factorial
  
function find_factorial($num1
                        $num2)
{
    // vector for store
    // the result
    $vec;
    $t = 0;
  
    // variable for store the
    // each number factorial
    $fac = 1;
  
    // copy of first number
    $temp = $num1;
  
    // found first
    // number factorial
    while (1) 
    {
        if ($temp == 1)
            break;
        $fac *= $temp;
        $temp--;
    }
      
    // push the first number
    // in result vector
    $vec[$t++] = $fac;
  
    // incerement the 
    // first number
    $num1++;
  
    // found the all reaming 
    // number factorial loop 
    // is working until all
    // required number 
    // factorial founded
    while ($num1 <= $num2
    {
        $fac *= $num1;
  
        // store the result 
        // of factorial
        $vec[$t++] = $fac;
  
        // incerement 
        // the first number
        $num1++;
    }
      
    // return the result
    return ($vec);
}
  
// function for 
// print the result
function print_format($result)
{
    // setw() is used for fill 
    // the blank right is used 
    // for right justification 
    // of data
    $x = count($result);
    $digits = strlen((string)$result[$x - 1]);
      
    for ($i = 0; $i < $x; $i++) 
    {
        echo str_pad($result[$i], ($digits + 1), 
                      " ", STR_PAD_LEFT) . "\n";
    }
}
  
// Driver Code
  
// number which found
// the factorial
// of between range
$m = 10;
$n = 20;
  
// store the result
// of factorial
$result_fac;
  
// function for 
// found factorial
$result_fac = find_factorial($m, $n);
  
// function for
// print format
print_format($result_fac);
  
// This code is contributed
// by mits
?>

chevron_right



Output :

             3628800
            39916800
           479001600
          6227020800
         87178291200
       1307674368000
      20922789888000
     355687428096000
    6402373705728000
  121645100408832000
 2432902008176640000


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : Mithun Kumar



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.