Big Factorial : C++

ruchit rawal
Sep 8, 2018 · 1 min read

Calculating factorials is one of the fundamental problems one encounters while begining with competitive programming , various variations of problems related to factorials is on many online judge platforms like(codechef,SPOJ etc).In this post we will learn how to calculate factorials of big numbers that can not be straight away stored in inbuilt datatypes like int,long etc.

A factorial is a mathematical operation defined as

n!=1*2*3*4.....(n-1)*n.

However this is not a good way because as numbers increase the value of factorials can easily surpass the limits of datatypes for example

40!=815915283247897734345611269596115894272000000000

which easily crosses int limit in C++ hence we need other ways to store the factorial.

The trick in these type of questions is to understand how to store the number , We can not store the answer in one single datatype that is why we use arrays to store digits of numbers.Consider this we can not store a 100 digit number in int but we can surely make a 100 size array and store each digit at the index.

Thus we can calculate factorials of big numbers using this algorithm . If u have any query or feedback do comment and let me know

code can be found out on my github

https://github.com/JARVVVIS/jarvvvis.github.io/tree/master/code

Thanks alot for reading ❤ .

Do check out my blog if u liked my writing

https://jarvvvis.github.io/

ruchit rawal

Written by

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade