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;
}
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;
}
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.
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() { }
.
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.
Your question consists of two parts:
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
).