Advertisement
Ritam_C

Factorials

Jan 31st, 2021
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.74 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main() {
  5.     int t;
  6.     cin >> t;
  7.     while(t--){
  8.         int n;
  9.         cin >> n;
  10.         int a[200];
  11.         a[0] = 1;
  12.         int carry = 0;
  13.         int d = 0;
  14.         for(int i = 1; i < 200; i++){
  15.             a[i] = 0;
  16.         }
  17.         for(int i = 1; i <= n; i++){
  18.             int j = 0;
  19.             int p = carry+i*a[j];
  20.             while(true){
  21.                 a[j] = p%10;
  22.                 carry = p/10;
  23.                 p = carry+i*a[j+1];
  24.                 if(p == 0 && j >= d){
  25.                     break;
  26.                 } else{
  27.                     j++;
  28.                 }
  29.             }
  30.             d = j;
  31.         }
  32.        
  33.         for(int i = d; i >= 0; i--){
  34.             cout << a[i];
  35.         }
  36.         cout << "\n";
  37.     }
  38.     return 0;
  39. }
  40.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement