n is of type int , and n*(n+1)/2 may overflow the range of int, so 1ll is multiplied with n, so that the result is of long long type (ll stands for long long).
Yes you can use typecasting. But, you should be careful to typecast at the proper place. If you only typecast the result of the expression, then, it will be too late get back your lost digits.