'
'Coded by [Digital-X-Treme]
'Q. How many positive integers less than 1,000,000 have exactly
' one digit equal to 9 and have a sum of digits equal to 13?
'
Option Explicit
Private Sub Form_Load()
Dim i As Long
Dim lngCount As Long
For i = 1 To 999999
DoEvents
If (OneDigitEqualToNine(i) And SumOfDigitsEqualToThirteen(i)) Then
lngCount = lngCount + 1
End If
Next i
Debug.Print "There are " & lngCount & " numbers between 0 and 999999 that have one digit equal to 9 and a sum of digits equal to 13."
End Sub
Private Function OneDigitEqualToNine(ByVal Number As Long) As Boolean
Dim strNumber As String
Dim i As Integer
Dim j As Integer
strNumber = CStr(Number)
j = 0
For i = 1 To Len(strNumber)
If Mid(strNumber, i, 1) = "9" Then
j = j + 1
End If
Next i
If j = 1 Then
OneDigitEqualToNine = True
Else
OneDigitEqualToNine = False
End If
End Function
Private Function SumOfDigitsEqualToThirteen(ByVal Number As Long) As Boolean
Dim strNumber As String
Dim i As Integer
Dim j As Integer
strNumber = CStr(Number)
j = 0
For i = 1 To Len(strNumber)
j = j + CInt(Mid(strNumber, i, 1))
Next i
If j = 13 Then
SumOfDigitsEqualToThirteen = True
Else
SumOfDigitsEqualToThirteen = False
End If
End Function