0% found this document useful (0 votes)
66 views27 pages

VB Practical

Practical Visual Basic

Uploaded by

scxmv5qksy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views27 pages

VB Practical

Practical Visual Basic

Uploaded by

scxmv5qksy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 27

Visual Basic Practical – BCA 5th Sem

Governme
nt PG
Collage
Practical Sector 9
Gurgaon
File
Visual Basic
Subject Code - BCA 304

Submitted Submitted
To By
Mrs. Sheela Mithlesh
Mam Kumar
Signature BCA 5th Sem
Sec-B
………………
Roll No. – 64
…………
Session 2024-25

1
Visual Basic Practical – BCA 5th Sem

Index
S.No. Content Page No. Date

1. WAP to perform arithmetic operation using 3-4


command buttons. (Declare variables globally).

2. Design an interface, which will appear like 5-6


marksheet. It will take input of marks in five
subjects and calculate total marks and
percentage then provide grade according to
following criteria. (Using nested if) (Use tab
index property to move focus).
If % Then Grade, > = 90 A+, > = 75 & < 90 A, > =
60 & < 75 B
> = 45 & < 60 C, Otherwise F
3. WAP to create a simple calculator (Using control 7-9
array)

4. Write a program to check whether an centered no. 10-11


is prime or not. (Using for loop & Exit for).

5. Write a program which will count all vowels, 12-13


consonants, digits, special characters and blank
spaces in a sentences (Using select case)

6. WAP to illustrate all functionalities of listbox and 14-16


combobox.

7. WAP using check boxes for following font effects. 17-19


Bold, Italic, Underline
Increase font size, Decrease font size, Font color

8. WAP to launch a rocket using pictures box and timer 20-21


control.

9. WAP to take input of two matrices and perform their 22-25


addition, subtraction and multiplication.

10. WAP to generate, print and find sum of first n 26-27


elements of fibonacci series using recursion using
user defined procedures..

2
Visual Basic Practical – BCA 5th Sem

Practical – 1
1. WAP to perform arithmetic operation using command buttons. (Declare variables
globally).
Public Class Form1

' Declare global variables

Dim num1 As Double

Dim num2 As Double

Dim result As Double

Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click

' Perform addition

num1 = CDbl(txtNum1.Text)

num2 = CDbl(txtNum2.Text)

result = num1 + num2

lblResult.Text = "Result: " & result

End Sub

Private Sub btnSubtract_Click(sender As Object, e As EventArgs) Handles btnSubtract.Click

' Perform subtraction

num1 = CDbl(txtNum1.Text)

num2 = CDbl(txtNum2.Text)

result = num1 - num2

lblResult.Text = "Result: " & result

End Sub

Private Sub btnMultiply_Click(sender As Object, e As EventArgs) Handles btnMultiply.Click

' Perform multiplication

num1 = CDbl(txtNum1.Text)

num2 = CDbl(txtNum2.Text)

result = num1 * num2

lblResult.Text = "Result: " & result

3
Visual Basic Practical – BCA 5th Sem

End Sub

Private Sub btnDivide_Click(sender As Object, e As EventArgs) Handles btnDivide.Click

' Perform division

num1 = CDbl(txtNum1.Text)

num2 = CDbl(txtNum2.Text)

If num2 <> 0 Then

result = num1 / num2

lblResult.Text = "Result: " & result

Else

lblResult.Text = "Error: Division by zero!"

End If

End Sub

End Class

Sample Output:

 Input: txtNum1 = 10, txtNum2 = 5


 Click Add Button: Displays Result: 15
 Click Subtract Button: Displays Result: 5
 Click Multiply Button: Displays Result: 50
 Click Divide Button: Displays Result: 2

4
Visual Basic Practical – BCA 5th Sem

Practical – 2
2. Design an interface, which will appear like marksheet. It will take input of marks in
five subjects and calculate total marks and percentage then provide grade according
to following criteria. (Using nested if) (Use tab index property to move focus).
If % Then Grade
> = 90 A+
> = 75 & < 90 A
> = 60 & < 75 B
> = 45 & < 60 C
Otherwise F
Public Class MarksheetForm

' Declare variables globally

Dim marks(4) As Double

Dim totalMarks As Double

Dim percentage As Double

Dim grade As String

Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click

' Take input from textboxes and store in the array

marks(0) = Convert.ToDouble(txtSubject1.Text)

marks(1) = Convert.ToDouble(txtSubject2.Text)

marks(2) = Convert.ToDouble(txtSubject3.Text)

marks(3) = Convert.ToDouble(txtSubject4.Text)

marks(4) = Convert.ToDouble(txtSubject5.Text)

' Calculate total marks

totalMarks = 0

For i As Integer = 0 To 4

totalMarks += marks(i)

Next

' Calculate percentage

percentage = (totalMarks / 500) * 100

' Determine grade using nested If

If percentage >= 90 Then

5
Visual Basic Practical – BCA 5th Sem

grade = "A+"

ElseIf percentage >= 75 Then

grade = "A"

ElseIf percentage >= 60 Then

grade = "B"

ElseIf percentage >= 45 Then

grade = "C"

Else

grade = "F"

End If

' Display total marks, percentage, and grade

lblTotalMarks.Text = "Total Marks: " & totalMarks.ToString()

lblPercentage.Text = "Percentage: " & percentage.ToString("0.00") & "%"

lblGrade.Text = "Grade: " & grade

End Sub

End Class

Sample Output:

 Input:
o txtSubject1 = 85
o txtSubject2 = 78
o txtSubject3 = 90
o txtSubject4 = 82
o txtSubject5 = 88
 After clicking the Calculate button:
o Total Marks: 423
o Percentage: 84.60%
o Grade: A

6
Visual Basic Practical – BCA 5th Sem

Practical – 3
3. WAP to create a simple calculator (Using control array)
Public Class Form1

Public Class CalculatorForm

' Declare variables

Dim num1 As Double

Dim num2 As Double

Dim operation As String

Private Sub btnDigit_Click(sender As Object, e As EventArgs)

Dim button As Button = CType(sender, Button)

txtDisplay.Text &= button.Text

End Sub

Private Sub btnOperation_Click(sender As Object, e As EventArgs)

Dim button As Button = CType(sender, Button)

num1 = Convert.ToDouble(txtDisplay.Text)

operation = button.Text

txtDisplay.Clear()

End Sub

Private Sub btnEquals_Click(sender As Object, e As EventArgs) Handles btnEquals.Click

num2 = Convert.ToDouble(txtDisplay.Text)

Dim result As Double

Select Case operation

Case "+"

result = num1 + num2

Case "-"

result = num1 - num2

Case "*"

result = num1 * num2

7
Visual Basic Practical – BCA 5th Sem

Case "/"

If num2 <> 0 Then

result = num1 / num2

Else

txtDisplay.Text = "Error"

Exit Sub

End If

End Select

txtDisplay.Text = result.ToString()

End Sub

Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click

txtDisplay.Clear()

num1 = 0

num2 = 0

operation = ""

End Sub

Private Sub CreateButtonArray()

' Create an array of button controls for digits and operations

Dim buttonTexts() As String = {"7", "8", "9", "/", "4", "5", "6", "*", "1", "2", "3", "-", "0", ".", "=",
"+"}

Dim xPos As Integer = 10

Dim yPos As Integer = 50

Dim btnWidth As Integer = 50

Dim btnHeight As Integer = 50

For i As Integer = 0 To buttonTexts.Length - 1

Dim btn As New Button

btn.Text = buttonTexts(i)

8
Visual Basic Practical – BCA 5th Sem

btn.Width = btnWidth

btn.Height = btnHeight

btn.Left = xPos

btn.Top = yPos

btn.Click += New EventHandler(AddressOf btnDigit_Click)

' Assign click events to operation buttons and the equals button

If btn.Text = "+" OrElse btn.Text = "-" OrElse btn.Text = "*" OrElse btn.Text = "/" Then

AddHandler btn.Click, AddressOf btnOperation_Click

ElseIf btn.Text = "=" Then

AddHandler btn.Click, AddressOf btnEquals_Click

End If

Me.Controls.Add(btn)

' Update xPos and yPos for positioning buttons

xPos += btnWidth + 5

If (i + 1) Mod 4 = 0 Then

xPos = 10

yPos += btnHeight + 5

End If

Next

End Sub

Private Sub CalculatorForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load

CreateButtonArray()

End Sub

End Class

9
Visual Basic Practical – BCA 5th Sem

Practical – 4
4. Write a program to check whether a centered no. is prime or not. (Using for loop &
Exit for).
Public Class PrimeCheckForm

Private Sub btnCheckPrime_Click(sender As Object, e As EventArgs) Handles btnCheckPrime.Click

' Get the input number from the textbox

Dim num As Integer = Convert.ToInt32(txtNumber.Text)

Dim isPrime As Boolean = True

' Check if the number is less than 2 (not prime)

If num < 2 Then

isPrime = False

Else

' Check for factors from 2 to the square root of the number

For i As Integer = 2 To Math.Sqrt(num)

If num Mod i = 0 Then

isPrime = False

Exit For ' Exit the loop as soon as a factor is found

End If

Next

End If

' Display the result

If isPrime Then

lblResult.Text = "The number " & num.ToString() & " is prime."

Else

lblResult.Text = "The number " & num.ToString() & " is not prime."

End If

End Sub

End Class

10
Visual Basic Practical – BCA 5th Sem

Sample Output:

 Input: 7
 Output: The number 7 is prime.
 Input: 12
 Output: The number 12 is not prime.

11
Visual Basic Practical – BCA 5th Sem

Practical – 5
5. Write a program which will count all vowels, consonants, digits, special characters
and blank spaces in a sentences (Using select case)
Public Class CharacterCounterForm

Private Sub btnCountCharacters_Click(sender As Object, e As EventArgs) Handles


btnCountCharacters.Click

' Get the input sentence from the textbox

Dim sentence As String = txtSentence.Text

Dim vowelCount As Integer = 0

Dim consonantCount As Integer = 0

Dim digitCount As Integer = 0

Dim specialCharCount As Integer = 0

Dim spaceCount As Integer = 0

' Iterate over each character in the sentence

For Each ch As Char In sentence

Select Case ch

Case "A"c, "E"c, "I"c, "O"c, "U"c, "a"c, "e"c, "i"c, "o"c, "u"c

vowelCount += 1

Case "B"c To "Z"c, "b"c To "z"c

consonantCount += 1

Case "0"c To "9"c

digitCount += 1

Case " "c

spaceCount += 1

Case Else

specialCharCount += 1

End Select

Next

' Display the counts

12
Visual Basic Practical – BCA 5th Sem

lblVowels.Text = "Vowels: " & vowelCount.ToString()

lblConsonants.Text = "Consonants: " & consonantCount.ToString()

lblDigits.Text = "Digits: " & digitCount.ToString()

lblSpaces.Text = "Spaces: " & spaceCount.ToString()

lblSpecialChars.Text = "Special Characters: " & specialCharCount.ToString()

End Sub

End Class

Sample Output:

 Input: Hello, World! 123


 Output:
o Vowels: 3
o Consonants: 7
o Digits: 3
o Spaces: 2
o Special Characters: 2

13
Visual Basic Practical – BCA 5th Sem

Practical – 6
6. WAP to illustrate all functionalities of listbox and combobox.

Public Class ListBoxComboBoxForm

Private Sub ListBoxComboBoxForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load

' Populate ComboBox with sample items

cmbBox.Items.Add("Apple")

cmbBox.Items.Add("Banana")

cmbBox.Items.Add("Orange")

cmbBox.Items.Add("Grapes")

' Populate ListBox with sample items

lstBox.Items.Add("Red")

lstBox.Items.Add("Green")

lstBox.Items.Add("Blue")

lstBox.Items.Add("Yellow")

End Sub

' Add item to ListBox

Private Sub btnAddToListBox_Click(sender As Object, e As EventArgs) Handles


btnAddToListBox.Click

If txtItem.Text <> "" Then

lstBox.Items.Add(txtItem.Text)

txtItem.Clear()

End If

End Sub

' Remove selected item from ListBox

Private Sub btnRemoveFromListBox_Click(sender As Object, e As EventArgs) Handles


btnRemoveFromListBox.Click

If lstBox.SelectedItem IsNot Nothing Then

14
Visual Basic Practical – BCA 5th Sem

lstBox.Items.Remove(lstBox.SelectedItem)

Else

MessageBox.Show("Please select an item to remove.")

End If

End Sub

' Display selected item from ListBox

Private Sub lstBox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles


lstBox.SelectedIndexChanged

If lstBox.SelectedItem IsNot Nothing Then

lblSelectedListBox.Text = "Selected Item: " & lstBox.SelectedItem.ToString()

End If

End Sub

' Add item to ComboBox

Private Sub btnAddToComboBox_Click(sender As Object, e As EventArgs) Handles


btnAddToComboBox.Click

If txtItem.Text <> "" Then

cmbBox.Items.Add(txtItem.Text)

txtItem.Clear()

End If

End Sub

' Remove selected item from ComboBox

Private Sub btnRemoveFromComboBox_Click(sender As Object, e As EventArgs) Handles


btnRemoveFromComboBox.Click

If cmbBox.SelectedItem IsNot Nothing Then

cmbBox.Items.Remove(cmbBox.SelectedItem)

Else

MessageBox.Show("Please select an item to remove.")

End If

End Sub

15
Visual Basic Practical – BCA 5th Sem

' Display selected item from ComboBox

Private Sub cmbBox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles


cmbBox.SelectedIndexChanged

lblSelectedComboBox.Text = "Selected Item: " & cmbBox.SelectedItem.ToString()

End Sub

End Class

Sample Output:

 Initial State:
o ListBox contains: Red, Green, Blue, Yellow.
o ComboBox contains: Apple, Banana, Orange, Grapes.
 Interaction:
o Add a new item ("Purple") to ListBox or ComboBox by typing in txtItem and
clicking the appropriate Add button.
o Select an item in ListBox or ComboBox to display it in the respective label.
o Click the Remove button to remove the selected item from the ListBox or
ComboBox.

16
Visual Basic Practical – BCA 5th Sem

Practical – 7
7. WAP using check boxes for following font effects.
Bold

Italic

Underline

Increase font size

Decrease font size

Font color

Public Class FontEffectsForm

' Initial font size

Private currentFontSize As Single = 12.0F

Private Sub chkEffects_CheckedChanged(sender As Object, e As EventArgs) Handles


chkBold.CheckedChanged, chkItalic.CheckedChanged, chkUnderline.CheckedChanged

ApplyFontStyle()

End Sub

Private Sub btnIncreaseFont_Click(sender As Object, e As EventArgs) Handles


btnIncreaseFont.Click

currentFontSize += 2.0F ' Increase font size by 2

ApplyFontStyle()

End Sub

Private Sub btnDecreaseFont_Click(sender As Object, e As EventArgs) Handles


btnDecreaseFont.Click

If currentFontSize > 2.0F Then ' Ensure font size doesn't go below 2

currentFontSize -= 2.0F ' Decrease font size by 2

ApplyFontStyle()

End If

End Sub

17
Visual Basic Practical – BCA 5th Sem

Private Sub btnFontColor_Click(sender As Object, e As EventArgs) Handles btnFontColor.Click

Dim colorDialog As New ColorDialog()

If colorDialog.ShowDialog() = DialogResult.OK Then

lblSampleText.ForeColor = colorDialog.Color

End If

End Sub

Private Sub ApplyFontStyle()

' Create a new FontStyle variable

Dim fontStyle As FontStyle = FontStyle.Regular

' Apply font styles based on checked boxes

If chkBold.Checked Then

fontStyle = fontStyle Or FontStyle.Bold

End If

If chkItalic.Checked Then

fontStyle = fontStyle Or FontStyle.Italic

End If

If chkUnderline.Checked Then

fontStyle = fontStyle Or FontStyle.Underline

End If

' Apply the new font style and size to the label

lblSampleText.Font = New Font(lblSampleText.Font.FontFamily, currentFontSize, fontStyle)

End Sub

End Class

Sample Output:

 Initial State: lblSampleText displays text with default font settings.


 Interactions:
o Check chkBold: The text in lblSampleText becomes bold.

18
Visual Basic Practical – BCA 5th Sem

o Check chkItalic: The text becomes italic.


o Check chkUnderline: The text becomes underlined.
o Click btnIncreaseFont: The text size increases.
o Click btnDecreaseFont: The text size decreases (not below size 2).
o Click btnFontColor: A color picker appears to select a new color for the text.

User Interface Design:

 CheckBoxes:
o chkBold labeled "Bold".
o chkItalic labeled "Italic".
o chkUnderline labeled "Underline".
 Buttons:
o btnIncreaseFont labeled "Increase Font Size".
o btnDecreaseFont labeled "Decrease Font Size".
o btnFontColor labeled "Change Font Color".
 Label:
o lblSampleText to display the text with applied effects.
 Form Layout:
o Place checkboxes and buttons conveniently for user interaction.

Position lblSampleText at the top or center for clear visibility of the changes.

19
Visual Basic Practical – BCA 5th Sem

Practical – 8

8. WAP to launch a rocket using pictures box and timer control

Public Class RocketLaunchForm

Private rocketSpeed As Integer = 5 ' Speed of the rocket movement

Private Sub RocketLaunchForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load

' Set initial position of the rocket (PictureBox)

picRocket.Top = Me.ClientSize.Height - picRocket.Height

End Sub

Private Sub btnStartLaunch_Click(sender As Object, e As EventArgs) Handles


btnStartLaunch.Click

tmrRocketLaunch.Enabled = True ' Start the timer

End Sub

Private Sub tmrRocketLaunch_Tick(sender As Object, e As EventArgs) Handles


tmrRocketLaunch.Tick

' Move the rocket up

picRocket.Top -= rocketSpeed

' Stop the rocket when it reaches the top of the form

If picRocket.Top + picRocket.Height < 0 Then

tmrRocketLaunch.Enabled = False

MessageBox.Show("Rocket has launched!")

End If

End Sub

End Class

20
Visual Basic Practical – BCA 5th Sem

Step-by-Step Setup:

1. Add a PictureBox to the form:


o Set its Name to picRocket.
o Assign an image of a rocket to the PictureBox.
o Position it at the bottom of the form.
2. Add a Timer control:
o Set its Name to tmrRocketLaunch.
o Set the Interval to a reasonable value (e.g., 50 milliseconds) for smooth
movement.
3. Add a Button to the form:
o Set its Name to btnStartLaunch and Text to "Launch Rocket".
4. Event Handlers:
o RocketLaunchForm_Load: Initializes the starting position of the rocket.
o btnStartLaunch_Click: Starts the timer to begin the launch.
o tmrRocketLaunch_Tick: Moves the rocket upward and stops when it reaches
the top.

Sample Output:

 Initial State: The rocket is displayed at the bottom of the form.


 Interaction:
o Click btnStartLaunch: The rocket moves upward.
 Final State:
o The rocket continues moving until it disappears off the top of the form.
o A message box shows "Rocket has launched!" once the rocket leaves the
visible area.

21
Visual Basic Practical – BCA 5th Sem

Practical – 9
9. WAP to take input of two matrices and perform their addition, subtraction and
multiplication.
Public Class MatrixOperationsForm

' Define the maximum size for the matrices

Const MAX_ROWS As Integer = 10

Const MAX_COLS As Integer = 10

' Declare matrices

Dim matrixA(MAX_ROWS - 1, MAX_COLS - 1) As Integer

Dim matrixB(MAX_ROWS - 1, MAX_COLS - 1) As Integer

Dim resultMatrix(MAX_ROWS - 1, MAX_COLS - 1) As Integer

Dim rows As Integer

Dim cols As Integer

Private Sub btnSetDimensions_Click(sender As Object, e As EventArgs) Handles


btnSetDimensions.Click

' Set matrix dimensions based on user input

rows = CInt(txtRows.Text)

cols = CInt(txtCols.Text)

' Clear previous matrix inputs

ClearMatrixInputs()

lblMessage.Text = "Enter values for Matrix A and Matrix B."

End Sub

Private Sub btnInputMatrices_Click(sender As Object, e As EventArgs) Handles


btnInputMatrices.Click

' Fill matrix A

For i As Integer = 0 To rows - 1

For j As Integer = 0 To cols - 1

matrixA(i, j) = CInt(InputBox($"Enter value for Matrix A[{i + 1},{j + 1}]:"))

22
Visual Basic Practical – BCA 5th Sem

Next

Next

' Fill matrix B

For i As Integer = 0 To rows - 1

For j As Integer = 0 To cols - 1

matrixB(i, j) = CInt(InputBox($"Enter value for Matrix B[{i + 1},{j + 1}]:"))

Next

Next

lblMessage.Text = "Matrices input completed. Perform operations.";

End Sub

Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click

For i As Integer = 0 To rows - 1

For j As Integer = 0 To cols - 1

resultMatrix(i, j) = matrixA(i, j) + matrixB(i, j)

Next

Next

DisplayResult("Addition Result", resultMatrix)

End Sub

Private Sub btnSubtract_Click(sender As Object, e As EventArgs) Handles btnSubtract.Click

For i As Integer = 0 To rows - 1

For j As Integer = 0 To cols - 1

resultMatrix(i, j) = matrixA(i, j) - matrixB(i, j)

Next

Next

DisplayResult("Subtraction Result", resultMatrix)

End Sub

23
Visual Basic Practical – BCA 5th Sem

Private Sub btnMultiply_Click(sender As Object, e As EventArgs) Handles btnMultiply.Click

' Initialize result matrix for multiplication

For i As Integer = 0 To rows - 1

For j As Integer = 0 To cols - 1

resultMatrix(i, j) = 0

Next

Next

For i As Integer = 0 To rows - 1

For j As Integer = 0 To cols - 1

For k As Integer = 0 To cols - 1

resultMatrix(i, j) += matrixA(i, k) * matrixB(k, j)

Next

Next

Next

DisplayResult("Multiplication Result", resultMatrix)

End Sub

Private Sub DisplayResult(operation As String, result(,) As Integer)

Dim resultText As String = operation & ":" & vbCrLf

For i As Integer = 0 To rows - 1

For j As Integer = 0 To cols - 1

resultText &= result(i, j).ToString() & vbTab

Next

resultText &= vbCrLf

Next

MessageBox.Show(resultText, operation)

End Sub

Private Sub ClearMatrixInputs()

txtRows.Clear()

24
Visual Basic Practical – BCA 5th Sem

txtCols.Clear()

End Sub

End Class

Sample Output:

 Initial State:
o User inputs dimensions (e.g., 2 rows and 2 columns).
 Matrix Input:
o User is prompted to enter values for Matrix A and Matrix B.
 Results:
o Upon clicking the operation buttons (Add, Subtract, Multiply), a message box
displays the resulting matrix.

User Interface Design:

 TextBoxes: For inputting the number of rows and columns.


 Buttons: For each step (setting dimensions, inputting matrices, performing
operations).
 Label: To show messages regarding the operations being performed.

Example Use Case:

1. Set dimensions (2 rows, 2 columns).


2. Input Matrix A:
o 1 2
o 3 4
3. Input Matrix B:
o 5 6
o 7 8
4. Click "Add": Displays 6 8 in a message box.
5. Click "Subtract": Displays -4 -4 in a message box.
6. Click "Multiply": Displays 19 22 in a message box.

25
Visual Basic Practical – BCA 5th Sem

Practical – 10
10. WAP to generate, print and find sum of first n elements of fibonacci series
using recursion using user defined procedures..
Public Class FibonacciSeriesForm

Private Sub btnGenerate_Click(sender As Object, e As EventArgs) Handles btnGenerate.Click

Dim n As Integer

' Get the number of elements to generate from user input

If Integer.TryParse(txtInput.Text, n) AndAlso n > 0 Then

Dim fibonacciNumbers As List(Of Integer) = GenerateFibonacci(n)

Dim sum As Integer = CalculateSum(fibonacciNumbers)

' Display the results

lblFibonacciSeries.Text = "Fibonacci Series: " & String.Join(", ", fibonacciNumbers)

lblSum.Text = "Sum of first " & n & " Fibonacci numbers: " & sum

Else

MessageBox.Show("Please enter a positive integer.")

End If

End Sub

' Function to generate Fibonacci series using recursion

Private Function GenerateFibonacci(n As Integer) As List(Of Integer)

Dim fibList As New List(Of Integer)

For i As Integer = 0 To n - 1

fibList.Add(Fibonacci(i))

Next

Return fibList

End Function

' Recursive function to calculate nth Fibonacci number

Private Function Fibonacci(n As Integer) As Integer

26
Visual Basic Practical – BCA 5th Sem

If n <= 1 Then

Return n

Else

Return Fibonacci(n - 1) + Fibonacci(n - 2)

End If

End Function

' Function to calculate the sum of a list of integers

Private Function CalculateSum(numbers As List(Of Integer)) As Integer

Dim sum As Integer = 0

For Each number As Integer In numbers

sum += number

Next

Return sum

End Function

End Class

Sample Output:

 Initial State: User enters a positive integer, say 5.


 Fibonacci Series: After clicking "Generate," the program displays:

Fibonacci Series: 0, 1, 1, 2, 3
Sum of first 5 Fibonacci numbers: 7

Example Use Case:

1. User enters 5 in the input box.


2. User clicks "Generate."
3. The output displays the Fibonacci series: 0, 1, 1, 2, 3 and the sum: 7.

27

You might also like