How to resolve the algorithm Middle three digits step by step in the VBA programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Middle three digits step by step in the VBA programming language

Table of Contents

Problem Statement

Write a function/procedure/subroutine that is called with an integer value and returns the middle three digits of the integer if possible or a clear indication of an error if this is not possible. Note: The order of the middle digits should be preserved. Your function should be tested with the following values; the first line should return valid answers, those of the second line should return clear indications of an error: Show your output on this page.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Middle three digits step by step in the VBA programming language

Source code in the vba programming language

Option Explicit

Sub Main_Middle_three_digits()
Dim Numbers, i&
    Numbers = Array(123, 12345, 1234567, 987654321, 10001, -10001, -123, -100, _
    100, -12345, 1, 2, -1, -10, 2002, -2002, 0)
    For i = 0 To 16
        Debug.Print Numbers(i) & " Return : " & Middle3digits(CStr(Numbers(i)))
    Next
End Sub

Function Middle3digits(strNb As String) As String
    If Left(strNb, 1) = "-" Then strNb = Right(strNb, Len(strNb) - 1)
    If Len(strNb) < 3 Then
        Middle3digits = "Error ! Number of digits must be >= 3"
    ElseIf Len(strNb) Mod 2 = 0 Then
        Middle3digits = "Error ! Number of digits must be odd"
    Else
        Middle3digits = Mid(strNb, 1 + (Len(strNb) - 3) / 2, 3)
    End If
End Function

  

You may also check:How to resolve the algorithm Literals/Integer step by step in the ALGOL W programming language
You may also check:How to resolve the algorithm Water collected between towers step by step in the Nim programming language
You may also check:How to resolve the algorithm Gray code step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm RCRPG step by step in the Wren programming language
You may also check:How to resolve the algorithm Super-Poulet numbers step by step in the Wren programming language