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

Visual Basic

This document is a practical file for a Visual Basic course at Maharshi Dayanand University, detailing various programming exercises. It includes an index of programs with their respective submission dates and signatures, along with code snippets for each program. The programs cover topics such as array manipulation, mathematical calculations, string operations, and user input handling.
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)
21 views27 pages

Visual Basic

This document is a practical file for a Visual Basic course at Maharshi Dayanand University, detailing various programming exercises. It includes an index of programs with their respective submission dates and signatures, along with code snippets for each program. The programs cover topics such as array manipulation, mathematical calculations, string operations, and user input handling.
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

MAHARSHI DAYANAND UNIVERSITY

AKIDO COLLEGE OF ENGINEERING


VISUAL BASIC PRACTICAL FILE
BCA – 305
Submitted To : Submitted By :
Aditya Kumar Pandey
Reg No. : 2213091152
Index
S.No List Of Program Date Signature
1 Static array iteration/add & find 30/08/202
particular value 4

2 Area of Rectangle 02/09/202


4
3 Simple Interest 09/09/202
4
4 2 D Array/iteration/add & find 11/09/2024
particular value

5 Switch case in VB 18/09/20


24
6 Fibonacci series up to 5 Numbers 20/09/202
4
7 No is even or odd 24/09/202
4
8 Add two numbers using function 27/09/202
4
9 Swap of two numbers using 04/10/202
function 4

10 Find the length of a string. 07/10/202


4
11 Call by value and call by 11/10/2024
reference

12 Create multiple forms in VB 16/10/202


4
13 Reverse of a number 21/10/20
24
14 Passing optional arguments in VB 23/10/202
4
15 To print 28/10/202
1 4
12
123
1234
12345
-
-
1- Static array iteration/add & find particular value Input
Module Module1
Sub Main()

' Declare a static array with a fixed size


Dim myArray(4) As Integer ' Static array with 5 elements
(index 0 to 4)
' Adding values to the array
myArray(0) = 10 myArray(1)
= 20 myArray(2) = 30
myArray(3) = 40 myArray(4)
= 50

' Iterating over the array


Console.WriteLine("Iterating over the array:")
For Each value As Integer In myArray
Console.WriteLine(value)

' Finding a particular value


Dim searchValue As Integer = 30
Dim found As Boolean = False

For Each value As Integer In myArray


If value = searchValue Then
Console.WriteLine($"Value {searchValue} found in the
array.")
found = True
Exit For ' Exit the loop once the value is found
End If
Next
If Not found Then
Console.WriteLine($"Value {searchValue} not found in the
array.")
End If
Console.ReadLine() ' Wait for user input to keep the console
open
End Sub
End Module
Ouput

2-Area of Rectangle Input


Module Module1
Sub Main()
' Declare variables for length and width
Dim length As Double
Dim width As Double
Dim area As Double
' Prompt the user for input
Console.Write("Enter the length of the rectangle: ")
length = Convert.ToDouble(Console.ReadLine())
Console.Write("Enter the width of the rectangle: ") width
= Convert.ToDouble(Console.ReadLine())
' Calculate the area
area = length * width
' Display the result
Console.WriteLine($"The area of the rectangle is: {area}")

' Wait for user input to close the console


Console.ReadLine()
End Sub
End Module
Output

3-Simple Interest Input


Module Module1
Sub Main()
' Variables to store principal, rate, and time
Dim principal As Double
Dim rate As Double
Dim time As Double
Dim simpleInterest As Double

' Input from the user


Console.WriteLine("Enter Principal Amount:")

principal = Convert.ToDouble(Console.ReadLine())
Console.WriteLine("Enter Annual Interest Rate (in %):")

rate = Convert.ToDouble(Console.ReadLine())
Console.WriteLine("Enter Time (in years):")

time = Convert.ToDouble(Console.ReadLine())

' Calculate Simple Interest


simpleInterest = (principal * rate * time) / 100

' Display the result


Console.WriteLine($"The Simple Interest is:
{simpleInterest}")
Console.ReadLine() ' Pause to view the result
End Sub
End Module
Output
4-2 D Array/iteration/add & find particular value Input
Module Module1
Sub Main()
' Declare a 2D array with fixed dimensions (3x3)
Dim my2DArray(2, 2) As Integer ' Rows: 3, Columns: 3

' Adding values to the 2D array


Dim value As Integer = 10
For i As Integer = 0 To 2
For j As Integer = 0 To 2
my2DArray(i, j) = value
value += 10 ' Increment value for demonstration
Next
Next

' Iterating over the 2D array


Console.WriteLine("Iterating over the 2D array:")
For i As Integer = 0 To 2
For j As Integer = 0 To 2
Console.Write($"{my2DArray(i, j)} ")
Next
Console.WriteLine()
Next

' Finding a particular value


Dim searchValue As Integer = 30
Dim found As Boolean = False

For i As Integer = 0 To 2
For j As Integer = 0 To 2
If my2DArray(i, j) = searchValue Then
Console.WriteLine($"Value {searchValue} found at
position ({i}, {j}).")
found = True
Exit For ' Exit the inner loop
End If
Next
If found Then Exit For ' Exit the outer loop
Next
If Not found Then
Console.WriteLine($"Value {searchValue} not found in the
2D array.")
End If
Console.ReadLine() ' Wait for user input to keep the console
open
End Sub
End Module
Output

-
5- Switch case in VB Input
Module Module

Sub Main()
' Declare a variable
Dim dayNumber As Integer

' Prompt the user for input


Console.WriteLine("Enter a number (1 to 7) to represent a day
of the week:")
dayNumber = Convert.ToInt32(Console.ReadLine())

' Use Select Case to determine the day of the week


Select Case dayNumber
Case 1
Console.WriteLine("Monday")
Case 2
Console.WriteLine("Tuesday")
Case 3
Console.WriteLine("Wednesday")
Case 4
Console.WriteLine("Thursday")
Case 5
Console.WriteLine("Friday")
Case 6
Console.WriteLine("Saturday")
Case 7
Console.WriteLine("Sunday")
Case Else
Console.WriteLine("Invalid input! Please enter a number
between 1 and 7.")
End Select

Console.ReadLine() ' Keep the console open


End Sub
End Module
Input

Output

-
6-Fibonacci series up to 5 Numbers Input
Module Module1

Sub Main()
' Variables to store the first two numbers of the Fibonacci
series
Dim num1 As Integer = 0
Dim num2 As Integer = 1

' Number of Fibonacci numbers to generate


Dim count As Integer = 5

Console.WriteLine("Fibonacci Series up to " & count & "


numbers:")

' Loop to generate the Fibonacci series


For i As Integer = 1 To count
Console.WriteLine(num1) ' Print the current number

' Calculate the next number in the series


Dim nextNum As Integer = num1 + num2
num1 = num2
num2 = nextNum
Next

Console.ReadLine() ' Wait for user input to keep the console


open
End Sub
End Module
Output
7-No is even or odd Input
Module Module1
Sub Main()
' Prompt the user to enter a number
Console.Write("Enter a number: ")
Dim input As String = Console.ReadLine()
' Validate the input
Dim number As Integer
If Integer.TryParse(input, number) Then
' Check if the number is even or odd
If number Mod 2 = 0 Then
Console.WriteLine($"{number} is Even.")
Else
Console.WriteLine($"{number} is Odd.")
End If
Else
Console.WriteLine("Invalid input. Please enter a valid
integer.")
End If
' Wait for user input before closing
Console.ReadLine()
End Sub
End Module
Output
8-Add two numbers using function Input
Module Module1
Sub Main()
' Declare two numbers
Dim num1 As Double
Dim num2 As Double

' Get input from the user


Console.Write("Enter the first number: ")
num1 = Convert.ToDouble(Console.ReadLine())

Console.Write("Enter the second number: ")


num2 = Convert.ToDouble(Console.ReadLine())

' Call the AddNumbers function and display the result


Dim result As Double = AddNumbers(num1, num2)
Console.WriteLine($"The sum of {num1} and {num2} is
{result}.")

Console.ReadLine() ' Wait for user input to keep the console


open
End Sub

' Function to add two numbers


Function AddNumbers(a As Double, b As Double) As Double
Return a + b
End Function
End Module
Input

Output
9-Swap of two numbers using function Input
Module Module1
Sub Main()
Dim num1 As Integer, num2 As Integer

Console.Write("Enter the first number: ")


num1 = Convert.ToInt32(Console.ReadLine())
Console.Write("Enter the second number: ")
num2 = Convert.ToInt32(Console.ReadLine())

Console.WriteLine($"Before Swap: num1 = {num1}, num2 =


{num2}")

' Call the Swap function


Swap(num1, num2)

Console.WriteLine($"After Swap: num1 = {num1}, num2 =


{num2}")
Console.ReadLine()
End Sub

Sub Swap(ByRef a As Integer, ByRef b As Integer)


Dim temp As Integer = a
a=b b = temp End
Sub
End Module

Input

Output
10-Find the length of a string.
Input
Module Module1
Sub Main()
Console.Write("Enter a string: ")
Dim input As String = Console.ReadLine()

Console.WriteLine($"The length of the string is


{input.Length}.")
Console.ReadLine()
End Sub
End Module

Input

Output

11-Call by value and call by reference Input


Module Module1
Sub Main()
Dim num As Integer = 5

Console.WriteLine($"Before Call by Value: num = {num}")


CallByValue(num)
Console.WriteLine($"After Call by Value: num = {num}")

Console.WriteLine($"Before Call by Reference: num =


{num}")
CallByReference(num)
Console.WriteLine($"After Call by Reference: num =
{num}")

Console.ReadLine()
End Sub

Sub CallByValue(ByVal n As Integer)


n += 10
End Sub

Sub CallByReference(ByRef n As Integer)


n += 10 End Sub
End Module

Input

Output
12-Create multiple forms in VB. Input
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs)
Handles Button1.Click
Dim form2 As New Form2()
form2.Show()
Me.Hide()
End Sub
End Class

Public Class Form2


Private Sub Button1_Click(sender As Object, e As EventArgs)
Handles Button1.Click
Dim form1 As New Form1()
form1.Show()
Me.Hide()
End Sub
End Class

13-Reverse of a number.
Input
Module Module1
Sub Main()
Console.Write("Enter a number: ")
Dim num As Integer = Convert.ToInt32(Console.ReadLine())
Dim reversed As Integer = ReverseNumber(num)

Console.WriteLine($"Reversed number: {reversed}")


Console.ReadLine()
End Sub

Function ReverseNumber(n As Integer) As Integer


Dim rev As Integer = 0
While n > 0
rev = rev * 10 + n Mod 10
n \= 10
End While
Return rev
End Function
End Module Input

Output

14-Passing optional arguments in VB Input


Module Module1
Sub Main()
' Calling AddNumbers with one argument
Console.WriteLine($"Addition result: {AddNumbers(10)}")
Console.ReadLine()
End Sub

Function AddNumbers(a As Integer, Optional b As Integer = 5)


As Integer
Return a + b
End Function
End Module
Input

Output

15-To print
1
12 123 1234 12345
Input
Module Module1
Sub Main()
For i As Integer = 1 To 5
For j As Integer = 1 To i
Console.Write(j)
Next
Console.WriteLine()
Next
Console.ReadLine()
End Sub
End Module
Output

You might also like