Computer Science, asked by mkjaiswal11, 6 hours ago

Can we decide what will be the length of an integer before it's initialization (or before giving it a value) in Java ? If yes, please tell how​

Answers

Answered by siddhipatil0
2

Answer:

Yes , we can find out what's the length of integer before its initialization.

Explanation:

Just take the base-10 log of the absolute value of the number, round it down, and add one? This works for positive and negative numbers that aren't 0, and avoids having to use any string conversion functions.

The log10, abs, and floor functions are provided by math.h. For example:

int nDigits = floor(log10(abs(the_integer))) + 1;

You should wrap this in a clause ensuring that the_integer != 0, since log10(0) returns -HUGE_VAL according to man 3 log.

Additionally, you may want to add one to the final result if the input is negative, if you're interested in the length of the number including its negative sign.

Similar questions