-2

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;
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189
Wael Jaber
  • 21
  • 5
  • 1
    Are you asking how to print (as in the title) or asking how to compute `3^1000`? Better to post what you have tried. – chux - Reinstate Monica Apr 26 '22 at 11:51
  • the accurate answer is how to compute and print * – Wael Jaber Apr 26 '22 at 11:53
  • Have a look at [this geekforgeeks page](https://www.geeksforgeeks.org/writing-power-function-for-large-numbers/), which appears to provide code to solve your problem in (amon others) c++. That should be adaptable to plain c in a straightforward way, if necessary, – collapsar Apr 26 '22 at 11:53
  • See [this question](https://stackoverflow.com/questions/66109785) and its answers. – Steve Summit Apr 26 '22 at 13:03

4 Answers4

2

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.

See also this question and its answer, and its linked duplicates.

Steve Summit
  • 45,437
  • 7
  • 70
  • 103
1

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() { }.

Darth-CodeX
  • 2,166
  • 1
  • 6
  • 23
1

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.

chqrlie
  • 131,814
  • 10
  • 121
  • 189
0

Your question consists of two parts:

  1. How to compute the result and write it to an array?
  2. How to print the result array?

The first half of the question has already been answered in the answers provided by other people. This answer provides a solution on how to print the result array, assuming that the array's elements are of type int (as they are in the question).

If every int element in the array is a number between 0 and 9 which represents a digit, then you can use the following function for printing the array:

void print_digits( const int array[], int array_size )
{
    int i = 0;

    //skip leading zeros, but don't skip last zero
    for ( ; i < array_size - 1; i++ )
    {
        assert( 0 <= array[i] && array[i] <= 9 );
        if ( array[i] != 0 )
            break;
    }

    //print all remaining digits
    for ( ; i < array_size; i++ )
    {
        assert( 0 <= array[i] && array[i] <= 9 );
        printf( "%d", array[i] );
    }

    printf( "\n" );
}

Here is a demonstration program:

#include <stdio.h>
#include <assert.h>

void print_digits( const int array[], int array_size )
{
    int i = 0;

    //skip leading zeros, but don't skip last zero
    for ( ; i < array_size - 1; i++ )
    {
        assert( 0 <= array[i] && array[i] <= 9 );
        if ( array[i] != 0 )
            break;
    }

    //print all remaining digits
    for ( ; i < array_size; i++ )
    {
        assert( 0 <= array[i] && array[i] <= 9 );
        printf( "%d", array[i] );
    }

    printf( "\n" );
}

int main( void )
{
    int digits[100] = {0};

    //set the number to 3.4 E+60
    digits[39] = 3;
    digits[40] = 4;

    print_digits( digits, sizeof digits / sizeof *digits );
}

This program has the following output:

3400000000000000000000000000000000000000000000000000000000000

Note that it is a bit of a waste of memory to use a 32-bit int to store a single digit between 0 and 9. Therefore, it would be less wasteful to use char instead, which is guaranteed to be able to represent numbers up to 127 (which is sufficient, because we only need to be able to represent numbers up to 9).

Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39