Home > OS >  Store/output a number bigger than unsigned long long can store
Store/output a number bigger than unsigned long long can store

Time:09-24

With user given number (n) for example n=5, I calculate the sum of 10000,10001,10002....99999. Works up until n=17, then I get a negative number or eventually a zero.

So my question is how do I store a number bigger than unsigned long long lets me

#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;
int main() 
{
  unsigned long long n, sum;
  cin>>n;
  unsigned long long start=pow(10, n-1);
  unsigned long long finish=pow(10, n)-1;
  sum=((finish-start 1)*(start finish))/2;
  cout<<sum<<endl;
  return 0;
}

CodePudding user response:

In case you don't want bother with installing some big number library like GMP, or you do it for some site like code wars then you have basically two options:

A) create your own data type to hold bigger numbers ( in this case not really recommended, as number may be insanely huge).

B) hold it as a string/char table and write a function to process adding values.

  • Related