Hi, I wrote a code in VBA for calculating the simple formula as gaussSum= [n*(n+1)] / 2, the number is given by the user. But the program does not perform the calculation it just gives me the number inputted by the user. How can I connect these two, so the program could take that number and then perform the calculation according to the formula?
Thanks in advance!
Option Explicit
Sub sumCalculation()
Dim userContribution As Integer
Dim gaussSum As Integer
userContribution = InputBox("Please state a number", "Need a number for Gauss Sum calculation")
gaussSum = calculate(userContribution)
printValue userContribution
End Sub
Function calculate(userContribution As Integer) As Integer
Dim result As Integer
result = userContribution * (userContribution + 1) / 2
calculate = result
End Function
Sub printValue(result As Integer)
Debug.Print "The Gauss Sum is " & result
End Sub
Thanks in advance!
Option Explicit
Sub sumCalculation()
Dim userContribution As Integer
Dim gaussSum As Integer
userContribution = InputBox("Please state a number", "Need a number for Gauss Sum calculation")
gaussSum = calculate(userContribution)
printValue userContribution
End Sub
Function calculate(userContribution As Integer) As Integer
Dim result As Integer
result = userContribution * (userContribution + 1) / 2
calculate = result
End Function
Sub printValue(result As Integer)
Debug.Print "The Gauss Sum is " & result
End Sub