I tried using the long type, but unfortunately, that didn’t work
long finalP = startingP * growthR;
As long as
growthR are still
int, this calculation will still be done using 32 bit integers (and only converted to 64 bit integers afterwards, when you already encountered overflow).
You can change all your variables to
long. Then you will be able to work with larger numbers, but eventually those will also overflow.
Is there a way for Java to print a large number without entering the negatives and ultimately the 0s
Depending on your application needs, you may need to look at either infinite-precision
BigInteger or floating point numbers such as
double (which can represent much larger quantities, but with a loss of precision in the lower digits — that seems acceptable here).
CLICK HERE to find out more related problems solutions.