How to receive an X number ranging from 0 to 10 100 in C?

Asked

Viewed 236 times

4

I have a question that can have as input a number X that can be from 0 until 10 100. I am receiving the input values/number as char to make it easier. But you can’t create a static string/array of char to make it all work. How to proceed?

  • 1

    Make a pile.

  • 1

    char X[101]; if (scanf("%100[0123456789]", X) != 1) /* erro */;

  • 7

    You mean 100 digits, right? 10 100 digits (1 googolplex) will need 4 x 10^75 yottabytes from memory... : P

  • 1

    You have contradictory information: (Title: up to 10 100 digits [0.. 10 10 100] is different from Body number of question: [0.. 10 100]) (difference between a googol and a googolpex)

  • Post more information about your problem, preferably by placing expected input examples.

  • Obg galera!!!!!

  • I think the biggest type of data you can use in c eh the "unsigned long long int" that goes from 0 to 18,446,744,073,709,551,615

Show 2 more comments

2 answers

1

How about progressively stock the number in a double?

#include <math.h> //pow()
int c;
long double valor = 0.0;
double potencia_de_dez = 0;
while ( (c = getchar()) != EOF )
{
    //Incrementa valor com o numero de entrada 
    valor += double(c - '0') * pow(10.0, potencia_de_dez);
    ++potencia_de_dez;
}
  • the c must be int! Besides while ( (c = getchar()) != 0 ) will, with enormous probability, generate an infinite loop. Replaces with while ( (c = getchar()) != EOF )

  • The example given was mainly given to show a way to address the problem. Taking into account that the type of input has not been defined, the use of the getchar may be inappropriate as well.

  • The problem with this approach is that this way, valor It’s the same when the input is 10 99 or 10 99 + 1 - you only keep the 53 most significant bits (which means we’re talking about 16 digits, give or take). Also, you’re reading the number backwards.

1

You can use a stack or a dynamic string to compose them (with malloc). Depending on the case, it’s better to use a large number library ready than to create one yourself.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.