double sum(double x){ return x*(x+1)/2.0; }
Note that I used “2.0” instead of “2”. This clearly indicates to the compiler (and user) that I am doing arithmetic with double instead of int.
int sum(int x){ return x*(x+1)/2; }