The sigma function computes the sum of integers from 1 to num:

N * (N+1) / 2

Write a program to compute sigma, implementing three functions that use different methods for computation. The first function (formulaSigma) will use the formula:

SumOfInts = num * (num+1) / 2

to compute the sum of integers. NOTE: Integer division is used.

The second function (loopSigma) will compute the sum using iteration (i.e. a loop).

The third function (recursiveSigma) will compute the sum using recursion.

All three function should take num as an argument and return the sigma value.

The main function should first explain to the user what the program will do.

It should then prompt the user for a positive integer or zero (num) to exit, error check it is zero or greater.

If the user does not enter 0 for num, then call the formulaSigma, loopSigma and recurSigma functions, using num as the input parameter for each.

The results of all three function calls should be printed out with descriptive text. Note that the results should be the same!

Sample Run:

Program to compute sigma (sum of integers 1 to num)

Enter a positive integer or zero to exit: 5

Formula result = 15
Iterative result = 15
Recursive result = 15

The program should loop and continue to compute results for new values of num, until the user enters 0 for the value of num.

The only way the user may exit the program is to enter 0 for the value of num -- do not ask the user whether to run the program again.

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.