Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which data type should i use to store upto 18 digits in a variable in c++?

Tags:

c++

c

I am trying to solve a code which requires me to input and output integer value upto 18 digits. Unfortunately i am unable to store the value in any data type. I have already tried

  long int
  unsigned long long 
  long long double,

None of these seem to work.Can u Please suggest me something that might help me output the value.

like image 866
amian Avatar asked Oct 28 '13 21:10

amian


People also ask

What data type should I use for 10 18?

www.geeksforgeeks.org/advanced-c-boost-library/amp/ There is a rule by which you can calculate how many digit a data type can store and the logic is 2^10 = 10^3 (nearly) i.e as long long int is an 64 bit integer, it can support 18 digit number because 2^64 = 10^18(nearly) by the above rule.

Which data type can store largest value in C?

The long long data type is overkill for just about every application, but C will let you use it anyway. It's capable of storing at least −9,223,372,036,854,775,807 to 9,223,372,036,854,775,807. Alternatively, get even more overkill with unsigned long long , which will give you at least 0 to 18,446,744,073,709,551,615.

Which data type can hold 10 digit integer numbers in C?

The INTEGER data type stores whole numbers that range from -2,147,483,647 to 2,147,483,647 for 9 or 10 digits of precision. The number 2,147,483,648 is a reserved value and cannot be used. The INTEGER value is stored as a signed binary integer and is typically used to store counts, quantities, and so on.

Can long long int store 10 18?

Unsigned long long can only store around 18 digits.

What data can be stored in a variable in C?

Data types in C decide what can be stored in a variable, and memory is allocated accordingly. For example, a variable can store a simple numeric digit in an int type variable, a letter of the alphabet in a char type variable, or a decimal number in a float type variable.

Which data type should I use to store a 12-digit integer?

Originally Answered: Which data type should I use to store a 12 digit integer in C++? Rule of thumb: you need 3 and a half bits (well, slightly less actually) to store a digit. Henceforth, for 12 digits 32 bits are not enough, but 64 bits are.

What data type can store whole numbers in C?

The int data type can store whole numbers from -2147483648 to 2147483647. In general, and in our tutorial, the int data type is the preferred data type when we create variables with a numeric value.

What is the basic data type in C?

char: The most basic data type in C. It stores a single character and requires a single byte of memory in almost all compilers. int: As the name suggests, an int variable is used to store an integer.


1 Answers

18 digits gives a maximum possible value of 999,999,999,999,999,999 ≈ 9.9 × 1017. This will fit into an unsigned, 64-bit integer (maximum value 264, which is about 1.8446744 × 1019). Try using the uint64_t type to ensure that you get this.

Hope this helps!

like image 188
templatetypedef Avatar answered Nov 13 '22 13:11

templatetypedef