Solution: Fix the Code

This lesson provides a solutions to the challenge given in the previous lesson.

Compilation error reason #

The following line causes a compilation error:

int amount = 10_000_000_000;

The problem here is that the value on the right-hand side is too large to fit in an int. According to the rules of integer literals, its type is long. For that reason, it doesn’t fit the type of the variable on the left-hand side. There are at least two solutions.

Solution 1 #

One solution is to leave the type of the variable to the compiler for example by the auto keyword:

Get hands-on with 1200+ tech skills courses.