How can I show the next 15 odd numbers of for example number 10 ?

Dani AI

Generated

Good question, and nice guidance from about using Mod. The key is to compute the first odd strictly after n, then output 15 values stepping by 2. You do not need an If block for even/odd; you can derive the start in one expression: start = n + 1 + (n Mod 2). That yields n+1 when n is even, and n+2 when n is odd. Then emit start + 2*k for k = 0..14. As showed, a loop is fine, but make sure you are not listing odds from 1..n; you want the next 15 after n.

VB6 example (prints to the Immediate window). For 10, this prints 11, 13, ..., 39.

Option Explicit

Private Sub Command1_Click()
    Dim n As Long, start As Long, k As Long
    If Not IsNumeric(Text1.Text) Then
        MsgBox "Please enter a number.", vbExclamation
        Exit Sub
    End If

    n = CLng(Text1.Text)
    start = n + 1 + (n Mod 2)

    For k = 0 To 14
        Debug.Print start + 2 * k
    Next k
End Sub

Python version, same idea:

n = int(input("Number: "))
start = n + 1 + (n % 2)
odds = [start + 2*i for i in range(15)]
print(odds)

Tip: If you need to display in a list box, replace the Debug.Print line with a call that adds each value to your UI control. Also validate input (empty or non-numeric) as shown, so your loop never runs on bad data.

Recommended Answers

All 7 Replies

Use the Mod Function to determine if input number is odd or not.
If not, add 1 to number.
Use for loop from 1 to 15 and a variable that is incremented by two with each iteration of the loop.
Take the value from the variable and concatinate into a string variable or textbox, or use the additem method of a list box to display.

Good Luck

Could you please write it i mean each steps?
coz I'm not an expert in VB

Please post your code that you are working on. Our experts will advice you to improve the same. Don't expect the complete code.

Read my reply line by line, sentence by sentence and you will have your answer as you code it up. Then as debasisdas said, if you have any problems with what you have created, then post it with where and what the error is.

Good Luck

There is no easy way if you are a beginner in the OOP way, Lida...
Yeah, you post your code and we'll see what we can do.

i think this may help you...

first, put 1 listbox(List1), 1 textbox(txtNum) and 1 command button(cmdShow)

and here's the code

Private Sub cmdShow_Click(Index As Integer)
Dim i, num As Integer

num = Val(txtNum.Text)

For i = 1 To num

If i Mod 2 = 1 Then

List1.AddItem i

End If
Next i
End Sub

Odd numbers will show on your listbox. If you want, return it with msgbox. >> Msgbox i

Well thanks everyone. I managed to solve it myself.
So here is how I did it:

Private Sub Command1_Click()
Dim x As Integer
x = Val(Text1.Text)
If x Mod 2 = 0 Then
x = x + 1
For i = 1 To 15
Print x
x = x + 2
Next i
Else
x = x + 2
For i = 1 To 15
Print x
x = x + 2
Next i
End If
End Sub

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.