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