Have the function MathChallenge(num) add up all the numbers from 1 to num. For example: if the input is 4 then your program should return 10 because 1 + 2 + 3 + 4 = 10. For the test cases, the parameter num will be any number from 1 to 1000.
Answers
Answered by
3
Answer:
Please specify the programming language used as well.
Attachments:
Answered by
1
Given below is the required function's program in Python programming language.
Explanation:
- The given function can be implemented using a for loop running for the same number of times as the number entered in the input.
- In this for loop we can add the numbers from 1 to 'num' to another variable 's' and save it back in 's' then finally, we return 's'.
- PROGRAM:
- def MathChallenge(num):
- n=num+1
- s=0
- for i in range(n):
- s=s+i
- return s
- num=int(input("Enter the number you wish to compute sum of: ))
- print("The sum 1 to ", num, " is ", MathChallenge(num))
Similar questions