public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
long number = 0;
String userInput;
boolean var = false;
System.out.print("Enter a number: ");
while (!var) {
try {
userInput = scanner.nextLine();
number = Long.parseLong(userInput);
if (number < 0) {
System.out.print("You should enter a number greater than or equal to zero!\n" +
"Please try again: ");
} else {
var = true;
}
} catch (NumberFormatException e) {
System.out.print("Error - You should enter a number!\nPlease try again: ");
}
}
BigInteger prod = BigInteger.ONE;
for (int i = 1; i <= number; i++) {
prod = prod.multiply(BigInteger.valueOf(i));
}
System.out.println(number + "! = " + prod);
}
}
I have this program for showing the factorial of a given number. It works fine but there is something that bothers me, I want to display very long numbers like this "1.551121004×10^25". Is there a method in java, or something else that I can use?