This code calculates the function f(x) = -1 + 2 + -3 + 4 + ... + (( - 1)^n)*n But when the input(n) is too big, like 1000000000, java doesn't display an output.
What can I do to solve this problem?
import java.util.Scanner;
public class Calculating_function {
public static void main(String[] args) {
Scanner input_taker = new Scanner(System.in);
String n_string = input_taker.nextLine();
long n = Long.parseLong(n_string);
System.out.println(fonk(n));
}
public static long fonk(long n) {
long total = 0;
for(long i = 1; i <= n; i++) {
total += (long)Math.pow(-1, i) * i;
}
return total;
}