File tree 1 file changed +48
-0
lines changed
1 file changed +48
-0
lines changed Original file line number Diff line number Diff line change
1
+ #include < iostream>
2
+ using namespace std ;
3
+
4
+ #define MAX 500
5
+
6
+ int multiply (int x, int res[], int res_size);
7
+
8
+ void factorial (int n)
9
+ {
10
+ int res[MAX];
11
+
12
+ res[0 ] = 1 ;
13
+ int res_size = 1 ;
14
+
15
+ for (int x=2 ; x<=n; x++)
16
+ res_size = multiply (x, res, res_size);
17
+
18
+ cout << " Factorial of given number is \n " ;
19
+ for (int i=res_size-1 ; i>=0 ; i--)
20
+ cout << res[i];
21
+ }
22
+ int multiply (int x, int res[], int res_size)
23
+ {
24
+ int carry = 0 ; // Initialize carry
25
+
26
+ for (int i=0 ; i<res_size; i++)
27
+ {
28
+ int prod = res[i] * x + carry;
29
+ res[i] = prod % 10 ;
30
+
31
+ carry = prod/10 ;
32
+ }
33
+
34
+ while (carry)
35
+ {
36
+ res[res_size] = carry%10 ;
37
+ carry = carry/10 ;
38
+ res_size++;
39
+ }
40
+ return res_size;
41
+ }
42
+
43
+ int main ()
44
+ { int n;
45
+ cin>>n;
46
+ factorial (n);
47
+ return 0 ;
48
+ }
You can’t perform that action at this time.
0 commit comments