{{#items}}

{{text}}

{{/items}} {{ctaText}}

{{#items}} {{/items}}

showing results for - "factorial in cpp of big numbers"

Oskar

31 Feb 2020

```
1// C++ program to compute factorial of big numbers
2#include<iostream>
3using namespace std;
4
5// Maximum number of digits in output
6#define MAX 500
7
8int multiply(int x, int res[], int res_size);
9
10// This function finds factorial of large numbers
11// and prints them
12void factorial(int n)
13{
14 int res[MAX];
15
16 // Initialize result
17 res[0] = 1;
18 int res_size = 1;
19
20 // Apply simple factorial formula n! = 1 * 2 * 3 * 4...*n
21 for (int x=2; x<=n; x++)
22 res_size = multiply(x, res, res_size);
23
24 cout << "Factorial of given number is
25";
26 for (int i=res_size-1; i>=0; i--)
27 cout << res[i];
28}
29
30// This function multiplies x with the number
31// represented by res[].
32// res_size is size of res[] or number of digits in the
33// number represented by res[]. This function uses simple
34// school mathematics for multiplication.
35// This function may value of res_size and returns the
36// new value of res_size
37int multiply(int x, int res[], int res_size)
38{
39 int carry = 0; // Initialize carry
40
41 // One by one multiply n with individual digits of res[]
42 for (int i=0; i<res_size; i++)
43 {
44 int prod = res[i] * x + carry;
45
46 // Store last digit of 'prod' in res[]
47 res[i] = prod % 10;
48
49 // Put rest in carry
50 carry = prod/10;
51 }
52
53 // Put carry in res and increase result size
54 while (carry)
55 {
56 res[res_size] = carry%10;
57 carry = carry/10;
58 res_size++;
59 }
60 return res_size;
61}
62
63// Driver program
64int main()
65{
66 factorial(100);
67 return 0;
68}
69
```

similar questions

factorial using recursion in c 2b 2bsum of two numbers c 2b 2bugly number code in c 2b 2bfind the biggest number from 3 numbers c 2b 2b 5cfrac 7b2 7d 7b5 7dmr 5e2 typed in c 2b 2bc 2b 2b print the amount of odd integer between n and mfactorial in c 2b 2bprime factorisation of a number in c 2b 2bprime numbers less than a given number c 2b 2bfast i 2fo in c 2b 2bhow to format big numbers with commas in c 2b 2bc 2b 2b factorialbigint c 2b 2bc 2b 2b fast i 2fofactorial c 2b 2b without using functionwrite a cpp program to calculate sum of first n natural numberscreate large integers with powers of 10 cppprogram to calculate factorial of number in c 2b 2b your answer for

you will get a confirmation link on this - you will have to click that for successful submission of your answer. we require this to keep the website free of spam, bots and unhelpful content

please ensure to add code which is syntactically corrent and executes properly

ask question on maxinterview

you will get a confirmation link on this - you will have to click that for successful submission of your question. we require this to keep the website free of spam, bots and unhelpful content

please be clear, to the point and respectful