Given three integers j, k and m
Build a function (in any development language) called SeqSummation, that calculates the sequence of summation as per:
j + (j + 1) + (j + 2) + (j + 3) + … + k + (k − 1) + (k − 2) + (k − 3) + … + m
Explanation: increment from j until it equals k, then decrement from k until it equals m.
Function Description
Create the function SeqSummation.
SeqSummation takes the following parameter(s):
• j integer
• k integer
• m integer
Program Output
• the value of the sequence sum
Example
• SeqSummation (5,9,6)
• i = 5, j = 9, k = 6
• Sum all the values from i to j and back to k: 5 + 6 + 7 + 8 + 9 + 8 + 7 + 6 = 56.
• Output: 56
Question
Imports System.IO
Module Module1
Function SeqSummation(ByVal j As Integer, ByVal k As Integer, ByVal m As Integer)
Dim sum As Integer = 0
For i = j To k
sum += i
Next
For i = k - 1 To m Step -1
sum += i
Next
Return sum
End Function
Sub Main()
Console.Write("Enter j: ")
Dim j As Integer = Integer.Parse(Console.ReadLine())
Console.Write("Enter k: ")
Dim k As Integer = Integer.Parse(Console.ReadLine())
Console.Write("Enter m: ")
Dim m As Integer = Integer.Parse(Console.ReadLine())
Console.WriteLine("Sum = " + SeqSummation(j, k, m).ToString())
Console.ReadLine()
End Sub
End Module
Comments
Leave a comment