usingMaths.com
From Theory to Practice - Math You Can Use.







<< PreviousNext >>

How to Generate Odd Numbers in Visual Basic for Kids



What are Odd Numbers? | Maths Explanation for VB.Net Kids

Odd numbers are numbers that are not divisible by 2.

They include:

1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, ...

In this beginner-friendly Maths Visual Basic tutorial for kids, we'll explore how to list odd numbers using VB.Net loops and conditions - perfect for primary school students learning to code.
To generate odd numbers in Visual Basic, we use a 'for' loop combined with a conditional statement. This simple Visual Basic exercise will help you understand number patterns and logic.

Create a new VB.Net class file; Project, Add Class.
Call it oddNumbers.vb.
Optionally, Create a new VB.Net module file; Project, Add Module.
Call it OddModule.vb.
Type out the adjoining Visual Basic (VB.Net) code for listing odd numbers.



Note: You can simply comment out the previous evenNumbers VB.Net object code from the main class or continue from where it stopped.



Code for Odd Number List with User Input in Visual Basic (VB.Net)

For a little more flexibility, let's add an input form to our VB.Net code for odd numbers.

All we need is a way to ask the user for input.
For this purpose, we'll use the Console.ReadLine() VB.Net library function.


So! Visual Basic Fun Practice Exercise - List Odd Numbers

As a fun practice exercise, feel free to try out your own boundary values, and see how the Visual Basic code lists the odd numbers between those boundary values.







VB.Net Code for Odd Numbers - Class File.

Public Class OddNumbers

    Dim first As Integer
    Dim last As Integer
    Dim result As New List(Of Integer)

    ' Simulate a constructor
    Public Sub _init_(alpha As Integer, omega As Integer)
        first = alpha
        last = omega
    End Sub

    ' Returns an list Of the desired Set Of even numbers
    Public Function prepResult() As List(Of Integer)
        ' Loop from start To Stop And rip out even numbers;

        Dim i = 0
        For i = first To last
            If i Mod 2 <> 0 Then ' modulo(%) Is explained later
                result.Add(i)
            End If
        Next i

        Return result
    End Function

End Class

VB.Net Code for Odd Numbers - Main Module.

Module Arithmetic_OddNumbers

    Sub Main()
        ' Use the even number Class
        Dim lower_boundary = 1
        Dim upper_boundary = 100
        Dim odd_list As New OddNumbers
        odd_list._init_(lower_boundary, upper_boundary)
        Dim answer = odd_list.prepResult()

        Console.WriteLine("Odd numbers between " & lower_boundary & " and " & upper_boundary & " are:")
        Console.Write(String.Join(", ", answer))

    End Sub

End Module

VB.Net Code for Odd Numbers - Main Module for Collecting Input.

Module Arithmetic_OddInput

    Sub Main()

        ' Collect Input
        Dim lower_boundary As Integer
        Dim upper_boundary As Integer

        Console.WriteLine("Enter the range for your odd numbers.")

        Console.Write("Enter Start Number: ")
        lower_boundary = CInt(Console.ReadLine())
        Console.Write("Enter Stop Number: ")
        upper_boundary = CInt(Console.ReadLine())

        ' Use the even number Class
        Dim odd_list As New OddNumbers
        odd_list._init_(lower_boundary, upper_boundary)
        Dim answer = odd_list.prepResult()

        Console.WriteLine("Odd numbers between " & lower_boundary & " and " & upper_boundary & " are:")
        Console.Write(String.Join(", ", answer))

    End Sub

End Module



<< PreviousNext >>