Why this code return negative result ??????? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why this code return negative result ???????

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

1st Apr 2020, 12:29 PM
Mohammad
Mohammad - avatar
9 Answers
+ 1
It's range is exceeding Integer range. For value 20... So maximum integer value +1 = minimum integer value.. Count goes from min negative value....
1st Apr 2020, 12:40 PM
Jayakrishna 🇮🇳
+ 4
beause 20! is equal to 2432902008176640000 which is bigger than the maximum value int data type can hold https://www.w3schools.com/cs/cs_data_types.asp
1st Apr 2020, 12:38 PM
Yanothai Chaitawat
Yanothai Chaitawat - avatar
+ 2
It's because the dataype is integer which store only 4byet you need to cast it to long or other data type
1st Apr 2020, 7:42 PM
Dawit Yitagesu
Dawit Yitagesu - avatar
+ 1
But why it works correctly for 25 25 bigger than 20 ??????
1st Apr 2020, 12:59 PM
Mohammad
Mohammad - avatar
+ 1
That's not the correct answer. Integers can be stored in only 4bytes. And max value of Integer is 2147483647. And 1 to this Integer number you get -2147483648. So if 20! is equal to 2432902008176640000 which is bigger than the maximum value int data type can hold, it showing only first 4bytes value here. static int ma(int x) { if(x == 1) { return 1; } return x * ma(x - 1); } static long ma(long x) { if(x == 1) { return 1; } return x * ma(x - 1); } static void Main(string[] args) { Console.WriteLine(ma(20)); //int 20 Console.WriteLine(ma(20L));//long 20 } Mohammad Run this code and see difference...
1st Apr 2020, 1:53 PM
Jayakrishna 🇮🇳
0
Ok i understand
1st Apr 2020, 2:03 PM
Mohammad
Mohammad - avatar
0
How do you mentioned me I cant mentioned i don't how
1st Apr 2020, 2:04 PM
Mohammad
Mohammad - avatar
0
While writing, Press @ and select name. There you can see all follower of this thread, all your followers, whom you following... Mohammad
1st Apr 2020, 2:06 PM
Jayakrishna 🇮🇳
0
Oh my god Thanks
1st Apr 2020, 2:16 PM
Mohammad
Mohammad - avatar