2

how to handle integers like 10^200 or larger than that in C language? it won't work even I use long long. then what should I do? I heard about big integer. but don't know how to work with it. as far i know it is a library function for C#. but I am working with C. is there any other way except big integer to work with such large integers? and can someone also explain me how to use big integer?

To clarify, I'm only looking for a solution that will work in C.

Sean McSomething
  • 6,376
  • 2
  • 23
  • 28
M Ashraful A
  • 627
  • 6
  • 13

3 Answers3

2

See similair question.

In short, there is no built-in type, but there are open source libraries having this ability: Boost.Multiprecision (Boost license) for C++ and GMP for C. (LGPL v3 / v2 dual license)

If for some reason (e.g. license incompatibility) you cannot use these libraries, here there are some tips if you are going to implement such functionality by yourself.

Community
  • 1
  • 1
Denis Itskovich
  • 4,383
  • 3
  • 32
  • 53
2

This is how we do by using integer arrays(though it is better to use character arrays).I have shown only addition , rest operation like comparison , multiplication subtraction you can write on your own.

#include<stdio.h>
#include<stdlib.h>
#define len 500 // max size of those numbers you are dealing

int findlength(int num[])
{
        int i=0;
        while(num[i]==0)
            ++i;
        return (len-i);


}


void equal(int num[] ,int a[])
{
        int i;

        for(i=0;i<len;++i)
            num[i]=a[i];

        free(a);

}


void print(int num[],int l)
{
        int i;

        for(i=len-l;i<len;++i)
            printf("%d",num[i]);

        printf("\n");

}


int *add(int num1[] , int num2[] )
{
        int i,carry=0;
        int *a = malloc(sizeof(int)*len); // an dynamic answer array has to be created because an local array will be deleted as soon as control leaves the function

        for(i=0;i<len;++i)
            a[i]=0;

        for(i=len-1;i>=0;--i)
        {
            a[i]=num1[i]+num2[i]+carry;
            carry=a[i]/10;
            a[i]=a[i]%10;
        }

        return a;

}


void input_number(int num[])
{
        int i=0,temp[len],j;
        char ch;

        for(i=0;i<len;++i) // fill whole array by zero. helps in finding length
            num[i]=0;

        i=0;

        printf("Enter number : ");

        while((ch=getchar())!='\n')
                temp[i++]= ch-'0'; //Saving number from left to right

        //shifting whole number to right side, now numbers are stored as 00000012 , 00000345 etc...

        for(j=0;j<=i;++j)
             num[len-1-j]=temp[i-j-1];


}

int main()
{
        int num1[len],num2[len],num3[len]; // to save space Use character array of size len.Char is also numeric type. It can hold 0- 9

        input_number(num1); // this way you can input those numbers
        input_number(num2);

        int len1=findlength(num1),len2=findlength(num2); // Might be used in Many operations.

        equal(num3,add(num1,num2));// This way define add , or subtract or any other operation you wan to do but return pointer to answer array.
        //Use equal function to equate "num3 = answer array" by some implementation.

        print(num3,findlength(num3)); // to print the number.
        // create an header file of all these function implementations and use them wherever you like

        return 0;
}
Number945
  • 4,631
  • 8
  • 45
  • 83
1

There is the concept of Arbitrary-precision arithmetic and there are a lot of libraries that can satisfy your requirements, usually those library are addressing Arbitrary-precision arithmetic with integers or with floating point numbers or with Fixed-point arithmetic .

You can find a lot of solutions for different platforms, licenses and languages, it depends on what you want to do in what kind of environment, but in general you will find a lot of options .

user2485710
  • 9,451
  • 13
  • 58
  • 102