0% found this document useful (0 votes)
27 views30 pages

Moinvb

The document contains 10 questions and answers about writing VB.NET programs using loops like Do Loop While and While loops. The programs cover topics like printing a name multiple times, printing sequences of numbers, calculating tables, checking if a number is Armstrong or palindrome, reversing numbers, and printing patterns like Floyd's triangle and inverted half pyramids. For each question, the code for the VB.NET program is provided, along with sample output.

Uploaded by

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

Moinvb

The document contains 10 questions and answers about writing VB.NET programs using loops like Do Loop While and While loops. The programs cover topics like printing a name multiple times, printing sequences of numbers, calculating tables, checking if a number is Armstrong or palindrome, reversing numbers, and printing patterns like Floyd's triangle and inverted half pyramids. For each question, the code for the VB.NET program is provided, along with sample output.

Uploaded by

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

Moin khan VB.

NET (404) SPID NO : 2021042078


S.Y. BCA (Sem-4)

Q.1 : write a vb.net program to read a name and print 5 times using "DO Loop
While"Loop.
Ans :
Module Module1

Sub Main()

Dim name As String = ""


Dim count As Integer = 1

Console.Write("Enter Name: ")


name = Console.ReadLine()
Do
Console.WriteLine("{0} ", name)
count = count + 1 Loop While count
<= 5
Console.WriteLine()

End Sub

End Module

Output :

Page [ 1 ]
Moin khan VB.NET (404) SPID NO : 2021042078
S.Y. BCA (Sem-4)

Q:2 Write a VB.Net program to print 1,11,31,61, ... using "Do Loop While" loop.
Ans:
Module Module1

Sub Main()
Dim num As Integer = 1
Dim cnt As Integer = 1
Dim dif As Integer = 10
Do
Console.Write(" {0} ", num)
num = num + dif dif = dif +
10 cnt = cnt + 1
Loop While cnt <= 5
Console.Write("press any key")
Console.ReadKey()
End Sub
End Module

OUTPUT:

Q:3 Write a VB.Net program to print the table of given number using "do loop while" loop.
Ans:
Module Module1
Sub Main()
Dim cnt As Integer = 1

Page [ 2 ]
Moin khan VB.NET (404) SPID NO : 2021042078
S.Y. BCA (Sem-4)

Dim num As Integer = 0


Console.Write("Enter Number: ")
num = Integer.Parse(Console.ReadLine())
Do
Console.Write(" {0} ", num * cnt)
cnt = (cnt + 1)
Loop While (cnt <= 10)
Console.WriteLine("press any key...")
Console.ReadKey()
End Sub
End Module

OUTPUT:

Q:4 Write a VB.Net program to check the given number is Armstrong number or not.
Ans:
Module Module1
Sub Main() Dim number As Integer = Error! Bookmark not defined.
Dim temp As Integer = 5
Dim re As Integer = 6

Dim res As Integer = 0

Page [ 3 ]
Moin khan VB.NET (404) SPID NO : 2021042078
S.Y. BCA (Sem-4)

Console.Write("Enter the number: ")


number = Integer.Parse(Console.ReadLine())
temp =
number
While (temp > 0)
re = (temp Mod 10) res =
res + (re * re * re) temp
= temp \ 10
End While
If (number = res) Then
Console.WriteLine("Number is armstrong")
Else
Console.WriteLine("Number is not armstrong")
End If
Console.WriteLine("press Any key...")
Console.ReadKey()
End Sub
End Module

OUTPUT:

Q:5 Write a VB.Net program to count the digits of given number using "While" loop.
Ans:
Module Module1

Sub Main() Dim num As Integer = 2


Dim cnt As Integer =
2

Console.Write("Enter the
number: ") num =

Integer.Parse(Console.Rea

Page [ 4 ]
Moin khan VB.NET (404) SPID NO : 2021042078
S.Y. BCA (Sem-4)

dLine()) 3
While (num > 0)
cnt = cnt + Error!
Bookmark not defined.
num = num
/ 10
End While
Console.WriteLine("Total number of digits are: {0}", cnt)
Console.WriteLine("press any key...")
Console.ReadKey()
End Sub
End Module

OUTPUT:

Q:6 Write a VB.Net program to check the given number is palindrome or not using the
"While" loop.

Ans:
Module Module1
Sub Main()
Dim number As Integer = 0
Dim a As Integer = 0
Dim b As Integer = 0
Dim temp As Integer = 0

Console.Write("Enter the number: ")


number = Integer.Parse(Console.ReadLine())
temp =
number

Page [ 5 ]
Moin khan VB.NET (404) SPID NO : 2021042078
S.Y. BCA (Sem-4)

While (number > 0)


a = number Mod 10 b
= b * 10 + a number
= number / 10
End While
If temp = b Then
Console.WriteLine("Number is palindrome")
Else
Console.WriteLine("Number is not palindrome")
End If
Console.WriteLine("press any key..")
Console.ReadKey()
End Sub
End Module

OUTPUT:

Q:7 Write a VB.Net program to reverse the given number using the "While" loop.
Ans :
Module Module1
Sub Main()
Dim num As Integer = 0
Dim re As Integer = 0
Dim rev As Integer = 0

Console.Write("Enter the number:


") num =
Integer.Parse(Console.ReadLine())
While (num > 0)
re = num Mod 10 rev
= rev * 10 + re num
= num / 10
End While
Console.WriteLine("Reverse: {0}", rev)
Console.WriteLine("press any key....")

Console.ReadKey()

Page [ 6 ]
Moin khan VB.NET (404) SPID NO : 2021042078
S.Y. BCA (Sem-4)

End Sub
End Module

OUTPUT:

Q:8 Write a VB.Net program to create the simple calculator using "select case".
Ans:
Module Module1
Sub Main()
Dim choice As Integer
Dim num1 As Integer = 0
Dim num2 As Integer = 0
Dim result As Integer = 0

Console.WriteLine("============================")
Console.WriteLine(" 1: Addition")
Console.WriteLine(" 2: Subtraction")
Console.WriteLine(" 3: Multiplication")
Console.WriteLine(" 4: Division")
Console.WriteLine("============================")
Console.Write("Enter choice: ")
choice = Integer.Parse(Console.ReadLine())

Console.Write("Enter number1: ")


num1 = Integer.Parse(Console.ReadLine())

Console.Write("Enter number2: ")

num2 = Integer.Parse(Console.ReadLine())

Page [ 7 ]
Moin khan VB.NET (404) SPID NO : 2021042078
S.Y. BCA (Sem-4)

Select Case choice


Case 1 result =
num1 + num2
Case 2
result = num1 -
num2 Case 3
result = num1 * num2
Case 3
result = num1 / num2
Case Else
Console.WriteLine("Invalid choice")
End Select
Console.WriteLine("Result: {0}", result)
Console.WriteLine("press any key......")
Console.ReadKey()
End Sub
End Module

OUTPUT:

Q:9 Write a VB.NET Program to print the Floyd's Triangle

Enter number of rows: 4

2 3

4 5 6

7 8 9 10

Ans:
Module Module1
Sub Main()
Dim n, i, j As Integer
Dim m As Integer = 1
Console.WriteLine("Enter the number of rows:
") n = Console.ReadLine()
Console.WriteLine()
For i = 1 To n
For j = 1 To i
Console.Write(" {0} ", m)

m += 1

Page [ 8 ]
Moin khan VB.NET (404) SPID NO : 2021042078
S.Y. BCA (Sem-4)

Next
Console.WriteLine("")
Next
Console.WriteLine("press any key...")
Console.ReadKey()
End Sub
End Module

OUTPUT:

Page [ 9 ]
Moin khan Sub : VB.NET (404) SPID NO : 2021042078
S.Y. BCA (Sem-4)

Q:10 Write a VB.NET Program to print an inverted half pyramid of numbers

Enter number of rows: 4

4 4 4 4

3 3 3

2 2

1 Ans:
Module Module1

Sub Main()
Dim n, i, j As Integer
Dim m As Integer = 1
Console.WriteLine("Enter the number of rows:
") n = Console.ReadLine() For i = n To 1
Step -1
For j = 1 To i
Console.Write("{0}", i)
Next
Console.WriteLine("")
Next
Console.WriteLine("press any key...")
Console.ReadKey()
End Sub
End Module

OUTPUT:

Page [ 10 ]
VB.NET (404) SPID NO : 2021042078
S.Y. BCA (Sem-4)

Q:11 Write a VB.NET Program to print an inverted half pyramid of numbers

Enter number of rows: 4

1 2 3 4

1 2 3

1 2

1 Ans:
Module Module1
Sub Main()
Dim n, i, j As Integer
Dim m As Integer = 1
Console.WriteLine(" Enter the number of rows: ")
n = Console.ReadLine()

For i = n To 1 Step -1
For j = 1 To i
Console.Write(" {0} ", j)
Next
Console.WriteLine("")
Next
Console.WriteLine(" Press any key to exit...")
Console.ReadKey()
End Sub
End Module

OUPUT:

Page [ 11 ]
Moin khan VB.NET (404) SPID NO : 2021042078
S.Y. BCA (Sem-4)

Q:12 Write a VB.NET Program to print half pyramid of numbers

Enter number of rows: 4

1 2

1 2 3

1 2 3 4

Ans:
Module Module1
Sub Main()
Dim a, b, num As Integer
Console.WriteLine(" Enter the number of rows: ")
num =
Console.ReadLine()
Console.WriteLine()
For a = 1 To num
For b = 1 To a
Console.Write("{0} ", b)
Next
Console.WriteLine("")
Next
Console.WriteLine(" Press any key to exit...")
Console.ReadKey()
End Sub
End Module

OUTPUT:

Page [ 12 ]
VB.NET (404) SPID NO : 2021042078
S.Y. BCA (Sem-4)

Q:13 Write a VB.NET Program to print right pyramid.

Enter number of rows: 4

* * * *

* * *

* *

Ans:
Module Module1
Sub Main()
Dim i, j, k, n, m As Integer
Console.WriteLine("Enter the rows")
n = Console.ReadLine() m = n
For i = 1 To n
For j = 1 To i
Console.Write(" ")
Next
For k = 1 To m
Console.Write("*") Next
m -= 1
Console.WriteLine("")
Next
Console.ReadKey()
End Sub
End Module

OUTPUT:

Page [ 13 ]
Moin khan VB.NET (404)

SPID:2021042078
S.Y. BCA (Sem-4)

Q:14 Write a VB.Net program to convert a double number to integer number.

Ans:
Module Module1
Sub Main()
Dim a As Double =
1245.678 Dim b As Integer
= 0 b = Int(a)
Console.WriteLine("value of a: {0}", a)
Console.WriteLine("value of b: {0}", b)
Console.ReadLine()
End Sub
End Module

OUTPUT:

Page [ 14 ]
Moin khan VB.NET (404) SPID NO : 2021042078
S.Y. BCA (Sem-4)

Q:15 Write a VB.Net program to print date and time in different using Format() function.

(General Date, Long Date, Short Date, Short Time)

Ans:
Module Module1
Sub Main()
Dim strDate As String
Dim D As Date
D = Date.Now
strDate = Format(D, "General Date")
Console.WriteLine(strDate) strDate =
Format(D, "Long Date")
Console.WriteLine(strDate) strDate =
Format(D, "Short Date")
Console.WriteLine(strDate) strDate =
Format(D, "Long Time")
Console.WriteLine(strDate) strDate
= Format(D, "Short Time")
Console.WriteLine(strDate)
Console.WriteLine("press any key to continue")
Console.ReadKey()
End Sub
End Module

OUTPUT:

VB.NET (404)
S.Y. BCA (Sem-4)

Page [ 15 ]
Moin khan

SPID NO :
2021042078
Q:16 Write a VB.Net program to print a number in different using Format() function.

(General Number, Fixed, Standard, Currency, Percent)

Ans:
Module Module1
Sub Main()
Dim result As String
Dim num As Integer = 123456789
result = Format(num, "General Number")
Console.WriteLine(result) result = Format(num,
"Fixed")
Console.WriteLine(result)
result = Format(num, "Standard")
Console.WriteLine(result) result
= Format(num, "Currency")
Console.WriteLine(result) result
= Format(num, "Percent")
Console.WriteLine(result)
Console.WriteLine("press any key to continue")
Console.ReadKey()
End Sub
End Module

OUTPUT:

Page [ 16 ]
Moin khan VB.NET (404) SPID NO : 2021042078
S.Y. BCA (Sem-4)

Q:17 Write a VB.NET program to compare two dates.

Ans:
Module Module1
Sub Main()
Dim date1 As New DateTime(2000, 5, 12)
Dim date2 As New DateTime(2003, 4, 23)
If (date1 < date2) Then
Console.WriteLine("{0} is less than {1}", date1, date2)
Else
Console.WriteLine("{0} is less than {1}", date2, date1)
End If
Console.WriteLine("press any key to continue")
Console.ReadKey()
End Sub
End Module

OUTPUT:

Page [ 17 ]
Moin khan VB.NET (404) SPID NO : 2021042078
S.Y. BCA (Sem-4)

Q:18 Write a VB.NET program to print yesterday’s date.

Ans:
Module Module1
Sub Main()
Dim today As DateTime = DateTime.Now
Dim yesterday As DateTime yesterday =
today - New TimeSpan(1, 0, 0, 0)

Console.WriteLine("Yesterday:{0}/{1}/{2}",yesterday.Day,
yesterday.Month, yesterday.Year)
Console.WriteLine("press any key to continue")
Console.ReadKey()
End Sub
End Module

OUTPUT:

Q:19 Write a VB.NET program to print tomorrow’s date.

Ans:
Module Module1
Sub Main()
Dim today As DateTime = DateTime.Now

Page [ 18 ]
Moin khan VB.NET (404) SPID
No:2021042078 S.Y. BCA (Sem-4)

Dim tomorrow As DateTime


tomorrow = today + New TimeSpan(1, 0, 0,
0)

Console.WriteLine("Tomorrow : {0}/{1}/{2}",
tomorrow.Day,tomorrow.Month, tomorrow.Year)
Console.WriteLine("press any key to continue")
Console.ReadKey()
End Sub
End Module

OUTPUT:

Q:20 Write a VB.NET program to print the difference of two times.

Ans:
Module Module1
Sub Main()
Dim time As TimeSpan
Dim ts1 As New TimeSpan(10, 30, 60)
Dim ts2 As New TimeSpan(10, 20, 70)

Page [ 19 ]
Moin khan VB.NET (404) SPID NO : 2021042078
S.Y. BCA (Sem-4)

time = ts1 -
ts2

Console.WriteLine("Hours:{0}, Minutes:{1}, Seconds:{2}",time.Hours,


time.Minutes, time.Seconds)

Console.WriteLine("press any key to continue")


Console.ReadKey()
End Sub
End Module

OUTPUT:

Q:21 Write a VB.NET program to print difference of two dates in days.

Ans:
Module Module1
Sub Main()
Dim diff As TimeSpan
Dim date1 As New DateTime(2000, 9, 20)
Dim date2 As New DateTime(2004, 2, 5)
diff = date2 -
date1
Console.WriteLine("Days: {0}", diff.TotalDays)
Console.WriteLine("press any key to continue")
Console.ReadKey()
End Sub

Page [ 20 ]
Moin khan VB.NET (404) SPID
No:2021042078 S.Y. BCA (Sem-4)

End Module

OUTPUT:

Q:22 Write a VB.NET program to calculate the area of a circle for given radius.

Ans:
Module Module1
Sub Main()
Dim radius As Single = 0.0F
Dim area As Single = 0.0F
Console.Write("Enter the radius:")
radius = Single.Parse(Console.ReadLine())
area = 3.14F * radius * radius

Console.WriteLine("Area of Circle: {0}", area)


Console.WriteLine("press any key to continue")
Console.ReadKey()
End Sub
End Module

Page [ 21 ]
Moin khan VB.NET (404) SPID NO : 2021042078
S.Y. BCA (Sem-4)

OUTPUT:

Q:23 Write a VB.NET program to calculate area of triangle.

Ans:
Module Module1
Sub Main()
Dim h As Double
Console.Write("ennter the hight of tringle:")
h = Console.ReadLine()
Dim l As Double
Console.Write("ennter the wight of tringle:")
l = Console.ReadLine()
Dim area As Double = 0.5 * h * l
Console.WriteLine("area of tringle:" & area)
Console.WriteLine("press any key...")
Console.ReadKey()
End Sub

End Module
OUTPUT:

Page [ 22 ]
Moin khan VB.NET (404) SPID
No:2021042078 S.Y. BCA (Sem-4)

Page [ 23 ]
Moin khan VB.NET (404) SPID NO : 2021042078

S.Y. BCA (Sem-4)

Q:24 Write a VB.NET program to calculate the sum of all elements of an integer array.
Ans:
Module Module1
Sub Main()
Dim arr As Integer() = New Integer(5) {}
Dim sum As Integer = 0
Console.WriteLine("Enter array elements: ")
For i As Integer = 0 To 4 Step 1
Console.Write("Element[{0}]: ", i)
arr(i) = Integer.Parse(Console.ReadLine())
sum = sum + arr(i)
Next
Console.WriteLine("Sum of array elements is: {0}", sum)
Console.WriteLine("press any key to continue")
Console.ReadKey()
End Sub
End Module

OUTPUT:

Page [ 24 ]
Moin khan VB.NET (404)
S.Y. BCA (Sem-4)

SPID NO :2021042

Q:25 Write a VB.NET program to find the largest element from the array of integers
Ans:
Module Module1
Sub Main()
Dim arr As Integer() = New Integer(5) {}
Dim large As Integer = 0
Console.WriteLine("Enter array elements: ")
For i As Integer = 0 To 4 Step 1
Console.Write("Element[{0}]: ", i)
arr(i) = Integer.Parse(Console.ReadLine())
If (large < arr(i)) Then
large = arr(i)
End If
Next
Console.WriteLine("Largest element in array is: {0}", large)
Console.WriteLine("press any key to continue")
Console.ReadKey()
End Sub
End Module

OUTPUT:

Page [ 25 ]
Moin khan VB.NET (404) SPID NO : 2021042078

S.Y. BCA (Sem-4)

Q.26 Write a VB.NET program to find the EVEN numbers from the array of integers .

Ans.
Module Module1
Sub Main()
Dim arr As Integer() = New Integer(5) {}
Console.WriteLine("Enter array elements: ")
For i As Integer = 0 To 4 Step 1
Console.Write("Element[{0}]: ", i) arr(i)
= Integer.Parse(Console.ReadLine())
Next
Console.WriteLine("EVEN Numbers are: ")
For i As Integer = 0 To 4 Step 1
If (arr(i) Mod 2) = 0 Then
Console.Write("{0} ", arr(i))
End If
Next
Console.WriteLine("press any key to continue")
Console.ReadKey()
End Sub
End Module

OUTPUT

Page [ 26 ]
Moin khan VB.NET (404) SPID NO : 2021042078
S.Y. BCA (Sem-4)

Q.27 Write a VB.NET program to reverse an integer array

ANS.
Module Module1
Sub Main()
Dim arr As Integer() = New Integer(5) {}
Dim rev_arr As Integer() = New Integer(5) {}
Dim i As Integer = 0
Dim j As Integer = 0
Console.WriteLine("Enter array elements: ")
For i = 0 To 4 Step 1
Console.Write("Element[{0}]: ", i)
arr(i) = Integer.Parse(Console.ReadLine())
Next
For i = 4 To 0 Step -1
rev_arr(j) = arr(i) j
= j + 1
Next
Console.WriteLine("Reversed array: ")
For i = 0 To 4 Step 1
Console.Write("{0} ", rev_arr(i))
Next
Console.WriteLine("press any key to continue")
Console.ReadKey()
End Sub
End Module

OUTPUT

Page [ 27 ]
Moin khan VB.NET (404) SPID NO : 2021042078

S.Y. BCA (Sem-4)

Q.28 Write a VB.NET program to find the prime number from the array of integers.
Ans.
Module Module1
Sub Main()
Dim arr As Integer() = New Integer(5) {}
Dim flag As Integer = 0
Dim i As Integer = 0
Dim j As Integer = 0

Console.WriteLine("Enter array elements: ")


For i = 0 To 4 Step 1
Console.Write("Element[{0}]: ", i)
arr(i) = Integer.Parse(Console.ReadLine())
Next
Console.WriteLine("Prime Numbers are: ")
For i = 0 To 4 Step 1
flag
= 0
For j = 2 To (arr(i) / 2) Step 1
If arr(i) Mod j = 0 Then
flag = 1
End If
Next
If flag = 0 Then
Console.Write("{0} ", arr(i))
End If
Next
Console.WriteLine("press any key...")
Console.ReadKey()
End Sub
End Module

OUTPUT:
Moin khan VB.NET (404) SPID NO : 2021042078
S.Y. BCA (Sem-4)

Page [ 28 ]

You might also like