-1

How can i represent(store) large numbers in C of length upto 100 digits or more?

Or alternatively.,

Is there any way to store a series in a variable(and not array since i have to make an array of series itself)

nalostta
  • 43
  • 1
  • 9
  • 1
    Please do refer this question in SO itself http://stackoverflow.com/questions/2252896/how-to-store-a-very-long-integer-value-in-a-c-program-for-an-exam-98474737475 – New Aug 10 '16 at 05:21
  • The methods presented in the link make performing operations quite cumbersome...but i guess its okay...ill figure it out. However do read the alternate question.... – nalostta Aug 10 '16 at 05:29
  • Which is it? C or Objective-C? Huge difference in your options. – rmaddy Aug 10 '16 at 05:34
  • C... Sorry my bad i was not aware of the difference.. – nalostta Aug 10 '16 at 05:35
  • 1
    Look up 'multi-precision arithmetic' and libraries such as [GMP](http://gmplib.org/) or the BN (big number) section of [OpenSSL](http://openssl.org/), etc. It also appears from the [tag:boost-multiprecision] tag that there is a Boost library in C++ for the job. – Jonathan Leffler Aug 10 '16 at 05:37
  • 1
    You know you can have arrays of arrays, right? Or arrays of pointers to arrays. – user253751 Aug 10 '16 at 05:39
  • @immibis But then representing the series as a number makes comparitively more sense as i have add certain digits to the series from both the right and the left...in an array, adding number is feasible for only one side..adding it from both the sides is i guess quite difficult... – nalostta Aug 10 '16 at 05:42
  • …and… For floating-point arithmetic, you might investigate [MPFR](http://www.mpfr.org/), and there's [MPC](http://www.multiprecision.org/) for complex arithmetic. The GMP, MPFR and MPC libraries are used by GCC. – Jonathan Leffler Aug 10 '16 at 05:43
  • 'Representing the series' — what are we dealing with here? It's beginning to sound like an [XY Problem](http://mywiki.wooledge.org/XyProblem). Are you dealing with numbers, or some sort of series, or something else? You can use list representations for a series, and you can easily add items at the start, end or middle of a list. And, to answer your alternative question: no — there isn't a way to avoid using an array or some similar structure to store massively multi-digit numbers with arbitrary precision. – Jonathan Leffler Aug 10 '16 at 05:45
  • @JonathanLeffler its a series but i chose to represent it as a really big number for the sake of convenience – nalostta Aug 10 '16 at 05:47
  • 1
    Convenience or inconvenience? At the moment, it sounds more like inconvenience. But I may just be out of my depth — I don't know what sort of series you're storing or how you store it as a single massively multi-digit number. That'd be the XY Problem aspects of your question springing to the fore. – Jonathan Leffler Aug 10 '16 at 05:49
  • @JonathanLeffler. Do u know abt codechef?... – nalostta Aug 10 '16 at 05:51
  • @JonathanLeffler codechef is a website ...theres a question m tryin to solve....it involves a series of numbers of whom i have to find permutations but the catch is that the permutations are guided by certain rules that make my life tough... – nalostta Aug 10 '16 at 05:57
  • OK; what I was thinking of as 'code chef' clearly isn't what you mean by it. – Jonathan Leffler Aug 10 '16 at 05:59

1 Answers1

-1

You can do it by storing the numbers as strings , here is an example for summing the numbers that are represnet that way (its in c++ but should be easy enough to convert it to c):

    string add (string &s1, string &s2){
    int carry=0,sum,i;

    string  min=s1,
    max=s2,
    result = "";

    if (s1.length()>s2.length()){
        max = s1;
        min = s2;
    } else {
        max = s2;
        min = s1;
    }

    for (i = min.length()-1; i>=0; i--){
        sum = min[i] + max[i + max.length() - min.length()] + carry - 2*'0';

        carry = sum/10;
        sum %=10;

        result = (char)(sum + '0') + result;
    }

    i = max.length() - min.length()-1;

    while (i>=0){
        sum = max[i] + carry - '0';
        carry = sum/10;
        sum%=10;

        result = (char)(sum + '0') + result;
        i--;
    }

    if (carry!=0){
        result = (char)(carry + '0') + result;
    }       

    return result;
}
Dr.Haimovitz
  • 1,568
  • 12
  • 16