Question: Please help me to write the code for problem 2, make sure it work Problem 2: The following code is an example of a FOR

Please help me to write the code for problem 2, make sure it work

Problem 2: The following code is an example of a FOR loop. The function finds the sum from 1 + 2 + 3 + x, where x is the input value. Paste the code below into Excel and modify the code so that it instead it adds 1^2 + 2^2 + 3^2 + x^2. Extra credit: Have it check to make sure that x is a positive integer before it does the loop, and tell the user that the input is invalid. Paste your updated code here.

Function LoopSum(x)

'this function takes a positive integer input and finds the sum from 1 to that positive integer 'for example, for input x = 5, the output will be 1+2+3+4+5, that is it will output the number 15.

LoopSum = 0 'start the output variable with a value of 0

For k = 1 To x

LoopSum = LoopSum + k 'each time it goes through the for loop, it adds the current value of k

Next k

End Function

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!