Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use 12 digit number in C?

Tags:

c

digit

I am dealing with a math example. I need to use 12 digit number for my code. So which datatype should i use, to use the number in my functions?

like image 526
DesperateCoders Avatar asked Aug 13 '10 11:08

DesperateCoders


People also ask

How big is a 12 digit number?

The smallest 12-digit number is 1 followed by 11 zeros. This number is called one hundred billion. The largest 12-digit number is 9 followed by another 11 nines. This number is called nine hundred ninety-nine billion nine hundred ninety-nine million nine hundred ninety-nine thousand nine hundred ninety-nine.

What is sum of digits in C?

Sum of digits in a C program allows a user to enter any number, divide that number into individual numbers, and sum those individual numbers. Example 1: Given number = 14892 => 1 + 4 + 8 + 9 + 2 = 24. Sum of digits of a given number “14892” is 24.

How do you count the number of digits in a long C?

The number of digits can be calculated by using log10(num)+1, where log10() is the predefined function in math.


3 Answers

If you have a 64-bit integer type, I'd go with that, since it gives you the (18 full digits) range:

−9,223,372,036,854,775,808 to
+9,223,372,036,854,775,807

For other tasks (even bigger integers or massive floating point values), I use GMP, the GNU multi-precision library. It's performance is impressive.

like image 98
paxdiablo Avatar answered Oct 27 '22 19:10

paxdiablo


64-bit integers (long, int64_t, unsigned long, uint64_t) should do the trick, or if you need decimals, double or long double.

like image 32
Delan Azabani Avatar answered Oct 27 '22 19:10

Delan Azabani


you can also use "unsigned long long" with format specifier "llu". It works fine for 12 digit number in C.

unsigned long long i=600851475143;
printf("%llu",i);
like image 37
Prashant Avatar answered Oct 27 '22 19:10

Prashant