You can take a loop from 2 to the inputted number and put increment of 2 and then add the value of the loop suppose x add it to s variable for sum and initialize it as 0 s=s+x;