Hello,
What is the difference between: parseLong() and valueOf() ?
For example:
String x = "1";
long y = Long.parseLong(x);
long z = Long.valueOf(x);
System.out.println(y + "\n" + z);
During this task I got confused, and finally I previewed solutions on how to apply this BigInteger.
Now my code looks like this:
BigInteger result = BigInteger.ONE;
if (n < 0) {
return "0";
} else if (n == 0) {
return "1";
} else if (n > 0 && n <= 150) {
int a = 1;
for (int i = 1; i <= n; i++) {
result = result.multiply((BigInteger.valueOf(i)));
//System.out.println(result); // check step by step
}
}
return String.valueOf(result);
package pl.codegym.task.task15.task1531;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigDecimal;
/*
Silnia
*/
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
int input = Integer.parseInt(reader.readLine());
reader.close();
System.out.println(factorial(input));
}
public static String factorial(int n) {
//tutaj wpisz swój kod
String result = null;
if (n < 0) {
return "0";
} else if (n == 0) {
return "1";
} else if (n > 0 && n <= 150) {
int a = 1;
for (int i = 1; i <= n; i++) {
a *= i;
//System.out.println(a);
}
result = Integer.toString(a);
}
return result;
}
}