0

how do i fix this?

import java.util.Scanner; class Demo{ public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int num = scanner.nextInt(); if (num < 0) { System.out.println("no"); } else { long factorial = 1; for (int i = 1; i <= num; i++) { factorial *= i; } System.out.println(factorial); } scanner.close(); } } Input : 14 Your Output : 87178291200 Expected Output : 1278945280 but in here my out put is correct , so what should i do

2nd Oct 2023, 1:42 AM
Usama Rizvi
Usama Rizvi - avatar
1 RĂ©ponse
0
It is good to see someone spot this bug and make a post here. I have the same problem and sent a bug report to SoloLearn weeks ago. After viewing your code I found the reason why test 1 is bugged even the code logic is correct. Factorial(14) returns a very large number: 87,178,291,200 The exercise expected we to us data type int, it's range is –2,147,483,648 to 2,147,483,647. It is clearly Factorial(14) can't fit in data type int. In fact, with Factorial(13), 6,227,020,800 it begin out of range and produce incorrect result. If you want to pass this exercise, you have to change variable factorial into a int. Yeah, it gives wrong answer in test case 1, but you can continue your study. https://code.sololearn.com/cZQM58KeTlAr/?ref=app
2nd Oct 2023, 2:42 AM
Wong Hei Ming
Wong Hei Ming - avatar