Vb Write a program to print multiples of 2 and 3 - vb.net

For my class, i need to write a program to find multiples of 2 and 3. The code i have would get me multiples of any number inputted into the program. My problem is that nothing is showing up in the message box that i've created and i don't know why. here's the code.
Public Class form1
Private Sub Button1_Click(ByVal Sender)
Dim Number1 As Integer
Dim Number2 As Integer
Dim Multiplier As Integer
Dim Answer As Integer
Dim i As Integer
Number1 = Val(TextBox1.Text)
Number2 = Val(TextBox2.Text)
Multiplier = 1
Do While Multiplier <= 10
For i = Number1 To Number2
Answer = i * Multiplier
ListBox1.Items.Add(i & "*" & Multiplier & "=" & Answer)
Next i
Multiplier = Multiplier + 1
Loop
End Sub
End Class
Any help at all would be appreciated.

I have not tested it but I think, this is what you looking for - all numbers that can be divided by 3 and 2 using multipliers from 1 to 10 over the range of numbers in your text boxes. In your code, I don't see where you weeding out your numbers that can be divided by 2 and 3
Private Sub Button1_Click(ByVal sender as Object, ByVal e as EventArgs) Handles Button1.Click
Dim num1 As Integer = Integer.Parse(TextBox1.Text)
Dim num2 As Integer = Integer.Parse(TextBox2.Text)
' may be need to check num2 > num1
Dim sum As Integer
For mult as Integer = 1 to 10
For i as integer = num1 To num2
total = i * mult
If sum Mod 2 = 0 OrElse sum Mod 3 = 0 Then
ListBox1.Items.Add(i.ToString() & " * " & mult & " = " & sum.ToString())
End If
Next i
Next
End Sub

This is my best guess as to what you are wanting. You said you were wanting multiples of 2 and 3 for any numbers given to the program, that that's what this does. If you wanted multiples of anything else, just add onto the part inside the {} in the coefficients declaration. Instead of using text boxes for input, I suggest using a NumericUpDowninstead; the GUI will be more intuitive to the end user.
Imports System.Text
Public Class Form1
Private Property maxNumb As Integer
Private Property minNumb As Integer
Private coefficients() As Integer = {2, 3}
Private Sub Button1_Click(sender As Button, e As EventArgs) Handles Button1.Click
Dim sb As New StringBuilder
For i = Me.minNumb To maxNumb Step 1
For Each coef As Integer In coefficients
sb.Append(i & " * ").Append(coef).Append(" = ").Append(i * coef)
Me.ListBox1.Items.Add(sb.ToString)
sb.Clear()
Next
Next
Me.ListBox1.Refresh()
End Sub
Private Sub NumericUpDown_ValueChanged(sender As NumericUpDown, e As EventArgs) Handles min_NumericUpDown1.ValueChanged, max_NumericUpDown2.ValueChanged
If sender.Name.Contains("max") Then
Me.maxNumb = sender.Value
Else
Me.minNumb = sender.Value
End If
End Sub
End Class

Related

Making a random array of numbers, with each number being unique

I'd like for a string of 24 numbers to appear in the text box, and then for each to have a random number from 0 to 23.
so far I have the following code made, but it makes a duplicate number appear occasionally.
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Randomize()
Dim Output As String = String.Empty
Dim NumberArray(24) As Integer
For n As Integer = 0 To 23
Dim RandomNumber As Integer = CInt(Int(Rnd() * 25) + 1)
NumberArray(n) = RandomNumber
Output &= "Index #" & n.ToString & Space(4) & NumberArray(n) & vbCrLf
Next
TextBox1.Text = Output
End Sub
End Class
any help greatly appreciated!

Sorting Numbers in Textbox in VB.Net

I need help with my program I want my output box to be in ascending order like:
0
1
2
3
4
5
6 and so on
but my output is like this 1234567890 and so on.
Here's my code:
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim count As Integer
If Integer.TryParse(TextBox1.Text, count) Then
Dim strNumbers As String = ""
For x As Integer = 0 To count - 1
strNumbers &= x
Next
TextBox2.Text = strNumbers
End If
End Sub
End Class
I believe I may have solved your problem :)
If you change textbox2 into a rich textbox, it will now allow you to output multiple lines
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim count As Integer
If Integer.TryParse(TextBox1.Text, count) Then
Dim strNumbers As String = ""
For x As Integer = 0 To count - 1
strNumbers &= x & vbNewLine & vbNewLine
Next
RichTextBox1.Text = strNumbers
End If
End Sub
Here is a little something I threw together to test the output

Fibonacci Sequence Visual Basic

I have a quick question about another Visual Basic assignment I'm working on. I have all the code and everything has gone smoothly so far. The app is meant to display the first 100 Fibonacci numbers in a list box, adding the two previously displayed numbers to get the next in a loop. The only problem is that when I hit the button to display the code, the loop continues, and doesn't just stop at 100 numbers. Where did I go wrong?
Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click
Dim dblA As Double = 0
Dim dblB As Double = 1
Dim dblC As Double
Dim intCounter As Integer
lstSequence.Items.Add(dblA.ToString)
lstSequence.Items.Add(dblB.ToString)
For intCounter = 1 To 100
dblC = dblA + dblB
dblA = dblB
dblB = dblC
lstSequence.Items.Add(dblC.ToString)
Next
End Sub
I just tried this. It works fine.
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim a As Integer = 0
Dim b As Integer = 1
Dim fib As Integer
Dim userinput, i As Integer
userinput = InputBox("how many interations?")
i = userinput
ListView1.Items.Add(1)
Do
fib = a + b
a = b
b = fib
ListView1.Items.Add(fib)
i = i + 1
Loop While fib < i
End Sub
End Class

VB.NET Help sectioning with distinct numbers

What I need is this: (A, B, C, D, etc.. are chars(name of the section))
A = 0-40
B = 41-80
C = 81-120
D = 121-160
and so on
I have a combobox containing numbers from 0 to 1040, when I select numbers such as 80, the section turns into C, which should be B because it goes inside 41-80 as seen above. As well as I choose 120, it turns into D, which should be C. etc etc. Only numbers from 1-40 works perfectly. Here is my code:
Dim counter As String
Dim mysection As String
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
counter = cmbYearAdd.SelectedItem
Dim a As Char = "A"
Dim distinct1% = 1, distinct2% = 40
For x As Integer = counter To 1040
If x >= distinct1 And x <= distinct2 Then
mysection = a
Exit For
Else
a = Chr(Asc(a) + 1)
distinct1 += 40
distinct2 += 40
End If
Next
lblSectionAdd.Text = "Section: " & mysection
End Sub
I have a label, combobox, and button.
This is because when the limits are raised the value of x is incremented when the for loop starts over.
But please don't try to fix it - this method is so awkward you shouldn't put any effort in it anymore. Simply keep the counter value fixed and divide by your section size to directly compute which section the value belongs to:
Dim counter As String
Dim mysection As String
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
counter = cmbYearAdd.SelectedItem
Dim a As Char = "A"
Dim sectionsize as Integer = 40
Dim sect as Integer
sect = Int((counter-1) / sectionsize)
mysection = Chr(Asc("A") + sect)
lblSectionAdd.Text = "Section: " & mysection
End Sub
The division result needs to be truncated to Integer, I'm using Int() for this.

Visual Basic (mistakes in code)

I'm new to Visual Studio.I tried to write a simple program in Visual Basic that takes a 13-digit number from a text box and writes its digits to an array.Then it writes the second member of the array (second digit of the number) to another text box, but it doesn't work. Here's the code:
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim array(12) As Integer
Dim index As Integer = 11
Dim code As Long = TextBox1.Text
Do While index >= 0
array(index) = code Mod 10
code /= 10
index -= 1
Loop
TextBox2.Text = array(1)
End Sub
End Class
Can you tell me what's wrong?
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim array(12) As Integer
Dim index As Integer = 11
Dim code As Char() = TextBox1.Text.ToCharArray()
For i As Integer = 0 To code.Count - 1
array(i) = Integer.Parse(code(i))
Next
TextBox2.Text = array(1)
End Sub