0

I am developing a single code that calculates Fatorial Number on C++.

The code

// Exemple:  5! = 5 x 4 x 3 x 2 x 1 = 120
#include <iostream>

using namespace std;

int main() {

    int number, total;
    cout << "Calculate fatorial number" << endl;
    cout << "-------------------" << endl << endl;
    cout << "Type a number... ";

    cin >> number;

    total = 1;
    for (int i = number; i > 0; i-- ){

       if (i == number){
           total = i * total;
           cout << number << "! = " << i << " x ";
       } else if (i > 1) {
             total = i * total;
             cout << i << " x ";
       } else {
             total = i * total;
             cout << i << " = ";
       }
    }

    cout << total;
    return 0;
}

The problem

When I give it numbers, do not return as expected.

What I want

I Want to know how bypass the bigger number problems so I can calculate at least 100!

Codes Output

  • number = 10; total = 3628800
  • number = 20 ; total = -2102132736

Compiler used

OnlineGDB

tadman
  • 208,517
  • 23
  • 234
  • 262
Andre Nevares
  • 711
  • 6
  • 21
  • 4
    Hint: Use a 64-bit integer if you need a larger [numeric range](https://en.cppreference.com/w/cpp/language/types). For numbers larger than that find a ["bignum" library](https://stackoverflow.com/questions/12988099/big-numbers-library-in-c). – tadman Jun 16 '20 at 23:05
  • 2
    You may want to read up on [Two's Complement](https://en.wikipedia.org/wiki/Two's_complement) if you're not familiar with it already. It explains why numbers "wrap" negative if they get "too big" and overflow. – tadman Jun 16 '20 at 23:07
  • You are overflowing the int, use unsigned int – Hjkl Jun 16 '20 at 23:07
  • Thanks a lot for the support! – Andre Nevares Jun 16 '20 at 23:39
  • @AndreNevares [See this example of boost multiprecision](https://ideone.com/e1sGBS) – PaulMcKenzie Jun 17 '20 at 00:12

3 Answers3

1

I guess you are searching for bignum arithmetic. You probably need to select some arbitrary-precision arithmetic library like GNU MP and use it.

codegorilla
  • 486
  • 3
  • 11
1

You need to either use an existing library that deals with large numbers, or implement your own. There's many options, gnu multi-precision, boost, etc...

If you choose to implement your own, you'll store digits in something like:

  • A string "90120304153543643626424262"
  • A std::vector<int> of digits (base 10) {9,0,1,2,0,....}
  • A std::vector<int> of digits (large base, for efficiency. 2^16 works well) {42567, 29183, 10987, ...}

Then, you'd need to roll your own multiplication, addition, assignment.

Jeffrey
  • 11,063
  • 1
  • 21
  • 42
0

hello the way i solve for large numbers is by doing this

#include <iostream>

using namespace std;
typedef unsigned long long int bigint; //big int 
int main() {

bigint total = 9494949494949497989;
cout<<total<<endl;

return 0;
}

so i make a custom data type called bigint and then instead of doing int total i do bigint total thus i am able to store large int values

kunz
  • 1,063
  • 1
  • 11
  • 27