Just declare somma do not initialize it as zero. "I" is always one. You have a while statement that checks against a static variable. Get rid of the while and calculate it directly as previously stated. If "I" was an array, such as 1,3,5,7,9, then a while would be applicable, but it is not an array, it is static.