How to resolve the algorithm Harshad or Niven series step by step in the VBA programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Harshad or Niven series step by step in the VBA programming language

Table of Contents

Problem Statement

The Harshad or Niven numbers are positive integers ≥ 1 that are divisible by the sum of their digits. For example,   42   is a Harshad number as   42   is divisible by   (4 + 2)   without remainder. Assume that the series is defined as the numbers in increasing order.

The task is to create a function/method/procedure to generate successive members of the Harshad sequence. Use it to:

Show your output here.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Harshad or Niven series step by step in the VBA programming language

Source code in the vba programming language

Option Explicit

Sub Main()
Dim i As Long, out As String, Count As Integer
   Do
      i = i + 1
      If IsHarshad(i) Then out = out & i & ", ": Count = Count + 1
   Loop While Count < 20
   Debug.Print "First twenty Harshad numbers are : " & vbCrLf & out & "..."
   
   i = 1000
   Do
      i = i + 1
   Loop While Not IsHarshad(i)
   Debug.Print "The first harshad number after 1000 is : " & i
End Sub

Function IsHarshad(sNumber As Long) As Boolean
Dim Summ As Long, i As Long, temp
   temp = Split(StrConv(sNumber, vbUnicode), Chr(0))
   For i = LBound(temp) To UBound(temp) - 1
      Summ = Summ + temp(i)
   Next i
   IsHarshad = sNumber Mod Summ = 0
End Function

  

You may also check:How to resolve the algorithm Display a linear combination step by step in the Java programming language
You may also check:How to resolve the algorithm Repeat a string step by step in the Oforth programming language
You may also check:How to resolve the algorithm Make directory path step by step in the Objeck programming language
You may also check:How to resolve the algorithm Ethiopian multiplication step by step in the Scheme programming language
You may also check:How to resolve the algorithm Zeckendorf number representation step by step in the Befunge programming language