In this code why do we get the result as a negative number, when we give the input as a long number such as 12345678901234567..? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 7

In this code why do we get the result as a negative number, when we give the input as a long number such as 12345678901234567..?

https://code.sololearn.com/ch2tC0DaK4V2/?ref=app

25th May 2019, 9:07 AM
🇮🇳 Hariharan 🇮🇳
7 Answers
+ 4
Because in c int max range is 2,147,483,647 after this it overflows and shows value in negative
25th May 2019, 9:22 AM
Puneet
+ 9
Becayse integers can only be this big before they overflow. The smaller number is a modulus of the bigger number with respect to the maximum integer.
25th May 2019, 9:10 AM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
25th May 2019, 9:16 AM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 4
Да базовые типы чисел обычно заточены под битность процессоров int8, int16, int32, int64, int128... но для длинных чисел можно использовать BCD - двоично-десятичный формат, его описание есть в сети. По сути там число представляется в виде массива байт. Есть два различных варианта BCD - BCD не упакованный, когда один байт это цифра от 0 до 9, она же символ от '0'-'9'. BCD упакованный, когда в одном байте две цифры, каждая занимает 4 бита, при чем значения обоих кварт-бит только в диапазоне от 0000 до 1001, то есть от 0 до 9. Есть библиотеки реализации таких чисел, но по сути можно самостоятельно реализовать все базовые операции над такими числами на основе школьного курса математики (сумма, разность, деление, умножение - более сложные всё равно состоят так или иначе из них).
26th May 2019, 10:59 PM
Michail Getmanskiy
Michail Getmanskiy - avatar
+ 3
Maybe another data type? You are using int, at least in java and c#, int max value is 2,147,483,647
25th May 2019, 9:11 AM
HNNX 🐿
HNNX 🐿 - avatar
+ 2
it totally depends the size of data type compiler supports both 32 and 64bit in 32 bit int has 32, 768 in 64 bit int has 2,147,483,648 so when you insert the value bigger than its capacity it shows garbage value. Hope this will help you
25th May 2019, 1:48 PM
Sankalp Hasija
Sankalp Hasija - avatar
+ 1
Tq all for ur answers😃😃
25th May 2019, 3:36 PM
🇮🇳 Hariharan 🇮🇳