Home > OS >  How can I compute a very big digit number like (1000 digits ) in c , and print it out using array
How can I compute a very big digit number like (1000 digits ) in c , and print it out using array

Time:04-27

how to print a number in C with 1000 digits same as 31000 ? using arr[1000], every position in the arr gets a digit.

void main() {
    unsigned int  Base, Power;
    int Digit[1000] = { 0 }, i;
    Base = 3;
    Power = 1000;
}

CodePudding user response:

Here is a very simple example to get you started. This is for addition (not multiplication or exponentiation), and it's for 3-digit numbers, not thousands. But it demonstrates the basic idea of holding each digit in one element of an array. When you add (or subtract, or multiply) numbers like these in a computer program, you can use exactly the same techniques you learned for doing arithmetic in school.

#include <stdio.h>

int main()
{
    int a[10], b[10], c[10];

    a[2] = 4; a[1] = 5; a[0] = 6;   /* 456 */
    b[2] = 7; b[1] = 8; b[0] = 9;   /* 789 */

    int partialsum = a[0]   b[0];
    c[0] = partialsum % 10;
    int carry = partialsum / 10;

    partialsum = a[1]   b[1]   carry;
    c[1] = partialsum % 10;
    carry = partialsum / 10;

    partialsum = a[2]   b[2]   carry;
    c[2] = partialsum % 10;
    carry = partialsum / 10;

    c[3] = carry;

    printf("%d%d%d%d\n", c[3], c[2], c[1], c[0]);
}

The biggest limitation in this program is that it's hardwired to work with 3-digit numbers and a 4-digit sum. The first improvement you might like to try to make would be to keep count (perhaps in additional variables) of the actual number of digits in each number.

CodePudding user response:

Assume Digit as an array (it is an array) (instead of a number), and then print those single digits using a loop.

As Digit is stored on stack memory, you can use sizeof() operator.

int length = sizeof(Digit) / sizeof(Digit[0]);
for(int i = 0; i < length; i  )
{
    printf("%d", Digit[i]);
}

Also, avoid void main() { }, use int main() { }.

CodePudding user response:

C does not have a standard multi-precision integer package. You can implement a brute force approach this way:

#include <stdio.h>

int multiply(char *num, int max, int p, int n) {
    int i, carry = 0;
    for (i = max; i > p;) {
        carry  = (num[--i] - '0') * n;
        num[i] = '0'   carry % 10;
        carry /= 10;
    }
    while (carry) {
        num[--i] = '0'   carry % 10;
        carry /= 10;
    }
    return i;
}

#define N        3
#define POWER    1000
#define NUMSIZE  1000

int main() {
    char num[NUMSIZE];
    int p = NUMSIZE;
    num[--p] = '\0';
    num[--p] = '1';

    for (int i = 0; i < POWER; i  ) {
        p = multiply(num, NUMSIZE - 1, p, N);
    }
    printf("%d^%d = %s\n", N, POWER, num   p);
    return 0;
}

Output:

3^1000 = 1322070819480806636890455259752144365965422032752148167664920368226828597346704899540778313850608061963909777696872582355950954582100618911865342725257953674027620225198320803878014774228964841274390400117588618041128947815623094438061566173054086674490506178125480344405547054397038895817465368254916136220830268563778582290228416398307887896918556404084898937609373242171846359938695516765018940588109060426089671438864102814350385648747165832010614366132173102768902855220001

31000 only has 478 digits, for 1000 digits, you need 32094 or 32095.

  • Related