Notes Pseudo Code Vs VB. NET
Notes Pseudo Code Vs VB. NET
Console.WriteLine("Enter Number")
Num = Console.ReadLine()
Page 1 of 38
Q.1 Write an algorithm in the form of pseudo code to input a number
and print whether the number is negative or positive. 0 is considered
as positive.
Pseudocode:
DECLARE Num : INTEGER
INPUT Num
IF Num >=0
THEN
OUTPUT “ Number is Positive”
ELSE
OUTPUT “ Number is Positive”
ENDIF
VB.NET
Dim Num As Integer
Console.WriteLine(" Please enter a number")
Num = Console.ReadLine()
Console.ReadKey()
Page 2 of 38
Q.2 Write an algorithm in the form of pseudo code to input a number and
print whether the number is Odd or Even. You may use MOD Operator.
Pseudocode:
DECLARE Num : INTEGER
INPUT NUM
IF Num MOD 2 = 0
THEN
OUTPUT “ Number is Even”
ELSE
OUTPUT “ Number is odd”
ENDIF
VB Code
Dim Num As Integer
Console.WriteLine(" Please enter a number")
Num = Console.ReadLine()
IF Num Mod 2 = 0 Then
Console.WriteLine("Number is Even")
Else
Console.WriteLine("Number is Odd")
End If
Console.ReadKey()
Page 3 of 38
Q.3 Write an algorithm in the form of pseudo code to input a number and
print whether the number is Odd or Even. You may use MOD Operator.
Pseudocode:
DECLARE Num : INTEGER
INPUT NUM
IF Num MOD 2 = 0
THEN
OUTPUT “ Number is Even”
ELSE
OUTPUT “ Number is odd”
ENDIF
VB Code
Console.ReadKey()
Page 4 of 38
Q.4 Write an algorithm to input grade and display Excellent if
grade is A, Display Good if grade is B , Display Average if
grade is C . Display improvement is need on other Grades.
PseudoCode:
DECLARE Grade : CHAR
INPUT Grade
IF Grade = ‘A’
THEN
OUTPUT “ Excellent”
ELSEIF Grade = ‘B’
THEN
OUTPUT “ Good”
ELSEIF Grade= ‘C’
THEN
OUTPUT “ Average”
ELSE
OUTPUT “ Improvement is needed”
ENDIF
Page 5 of 38
VB.Net Code:
Dim Grade As Char
Console.WriteLine("Please enter Grade")
Grade = Console.ReadLine()
If Grade = "A" Then
Console.WriteLine("Excellent")
ElseIf Grade = "B" Then
Console.WriteLine("Good")
ElseIf Grade = "C" Then
Console.WriteLine("Average")
Else
Console.WriteLine("Improvement is needed")
End If
Console.ReadKey()
Pseudo code:
Page 6 of 38
Vb.Net
Console.ReadKey()
Page 7 of 38
Q Write an algorithm to input grade and diplay Excellent if grade is A,
Display Good if grade is B , Display Average if grade is C . Display
improvement is need on other Grades.
DECLARE Grade : CHAR
DECLARE Grade : CHAR
INPUT Grade
INPUT Grade
IF Grade = ‘A’
CASE OF Grade
THEN
‘A’ OR ‘a’ : OUTPUT “ Excellent”
OUTPUT “ Excellent”
‘B’ OR ‘b’ : OUTPUT “ Good”
ELSEIF Grade = ‘B’
‘C’ OR ‘c’ : OUTPUT “ Average”
THEN
OTHERWISE OUTPUT “ Improvement is needed”
OUTPUT “ Good”
END CASE
ELSEIF Grade= ‘C’
THEN
OUTPUT “ Average”
ELSE
OUTPUT “ Improvement is needed”
ENDIF
Page 8 of 38
VB.Net Code:
Dim Grade As Char
Q. Write an algorithm to input day of the week from 1 to 7 and display its
equalent day of the week. E.g if User input 1 then display Monday, User Input 2,
Display Tuesday and so on… Also display an error message if number is invalid.
Pseud Code: DECLARE Day : Integer
DECLARE Day : INTEGER INPUT Day
INPUT Day
IF Day=1 CASE OF Day
THEN
OUTPUT “ Monday” 1 : OUTPUT “ Monday
ELSEIF Day=2 2 : OUTPUT “ Tuesday”
THEN
OUTPUT “ Tuesday” 3 : OUTPUT “ Wednesday”
ELSEIF Day=3
THEN 4 : OUTPUT “ Thursday”
OUTPUT “ Wednesday” 5 : OUTPUT “ Friday”
6 : OUTPUT “ Saturday”
ELSEIF Day=4
THEN 7 : OUTPUTn” Sunday”
OUTPUT “ Thursday” OTHERWISE OUTPUT “ Invalid Day”
END CASE
Page 9 of 38
ELSEIF Day=5
THEN
OUTPUT “ Friday”
ELSEIF Day=6
THEN
OUTPUT “ Saturday”
ELSEIF Day=7
THEN
OUTPUT “ Sunday”
ELSE
OUTPUT “ Invalid Day”
ENDIF
Console.ReadKey()
Page 10 of 38
VB.Net Code with SELECT CASE Statement:
Dim Day As Integer
Console.ReadKey()
LOOP:
Pseudo Code:
Total 0
FOR Count 1 TO 10
INPUT Marks
Total Total + Marks
NEXT
Avg Total / 10
OUTPUT Total , Avg
Page 11 of 38
VB.NET Code:
Total = 0
For Count = 1 To 10
Console.WriteLine("Please eneter marks")
Marks = Console.ReadLine()
Total = Total + Marks
Next
Avg = Total / 10
Console.WriteLine("Total marks = " & Total)
Console.WriteLine("Average marks= " & Avg)
Console.ReadKey()
Pseudo Code:
Total 0
Count 1
WHILE Count <= 10
INPUT Marks
Total Total + Marks
Count Count + 1
ENDWHILE
Avg Total / 10
OUTPUT Total , Avg
Page 12 of 38
VB.Net Code:
Total = 0
Count = 1
While Count <= 10
Console.WriteLine("Please eneter marks")
Marks = Console.ReadLine()
Total = Total + Marks
Count = Count + 1
End While
Avg = Total / 10
Console.WriteLine("Total marks = " & Total)
Console.WriteLine("Average marks= " & Avg)
Console.ReadKey()
Q Write an algorithm to input marks of computer subject of 10 students of
a class. Print overall total and average of the whole class.
Using REPEAT …UNTIL loop.
Pseudo Code:
Total 0
Count 1
REPEAT
INPUT Marks
Total Total + Marks
Count Count + 1
UNTIL Count > 10
Page 13 of 38
Avg Total / 10
OUTPUT Total , Avg
VB.NET Code:
' This program is developed by talat jahangir on 15th July 2024 at 3:41 P.m
Total = 0
Count = 1
Do ' in Pseudo Code REPEAT
Console.WriteLine("Please eneter marks")
Marks = Console.ReadLine() ' in Pseudo Code INPUT Marks
Total = Total + Marks
Count = Count + 1
Loop Until Count > 10 ' in Pseudo Code UNTIL Count > 10
Avg = Total / 10
Console.WriteLine("Total marks = " & Total)
Console.WriteLine("Average marks= " & Avg)
Console.ReadKey()
Pseudo Code:
INPUT Marks
WHILE Marks <> -1
NPUT Marks
ENDWHILE
Page 14 of 38
VB.Net Code:
Dim Marks As Integer
Console.ReadKey()
Pseudo Code:
REPEAT
NPUT Marks
UNTIL Count = -1
VB.Net Code :
Do
Console.ReadKey()
Page 15 of 38
Algorithm Techniques:
1. How Many
Q.1. Input ten numbers and print how many numbers are positive and how
many numbers are negative? (Using FOR ….To ….NEXT. 0 is considered as
positive.
Pseudo Code:
Pos 0
Neg 0
FOR Count 1 TO 10
INPUT Num
IF Num >= 0 THEN
Pos Pos + 1
ELSE
Neg Neg + 1
ENDIF
NEXT
OUTPUT Neg , Pos
Page 16 of 38
VB Code:
Dim Num As Integer
Dim Pos As Integer
Dim Neg As Integer
Dim Count As Integer
Neg = 0
Pos = 0
For Count = 1 To 10
Console.WriteLine("Enetr Number")
Num = Console.ReadLine()
If Num >= 0 Then
Pos = Pos + 1
Else
Neg = Neg + 1
End If
Next
Console.WriteLine("Negatve numbers are " & Neg)
Console.WriteLine("Positive numbers are " & Pos)
Console.ReadKey()
Neg = 0
Pos = 0
Count = 1
While Count <= 10
Console.WriteLine("Enetr Number")
Num = Console.ReadLine()
If Num >= 0 Then
Pos = Pos + 1
Else
Neg = Neg + 1
End If
Count= Count + 1
End While
Console.ReadKey()
Page 17 of 38
Same code Using REPEAT Loop:
Neg = 0
Pos = 0
Count = 1
Do
Console.WriteLine("Enetr Number")
Num = Console.ReadLine()
If Num >= 0 Then
Pos = Pos + 1
Else
Neg = Neg + 1
End If
Count = Count + 1
Loop Until Count > 10
Console.ReadKey()
Page 18 of 38
Unknown Number of Repetition:
Pseudo Code:
Less1000 0
Greater1000 0
INPUT Num
WHILE Num <> -1
INPUT Num
ENDWHILE
Page 19 of 38
Pseudo code using REPEAT Loop:
Less1000 0
Greater1000 0
REPEATE
INPUT Num
UNTIL Num = -1
Page 20 of 38
VB.Net Code using WHILE Loop:
Dim Num As Integer
Dim Less1000 As Integer
Dim Greater1000 As Integer
Less1000 = 0
Greater1000 = 0
Using DO..LOOP…UNTIL
Less1000 = 0
Greater1000 = 0
Do
Console.WriteLine(" Please enter number")
Num = Console.ReadLine()
If Num < 1000 Then
Less1000 = Less1000 + 1
Else
If Num > 1000 Then
Greater1000 = Greater1000 + 1
End If
End If
Loop Until Num = -1
Console.WriteLine("Less than 1000 Numers = " & Less1000)
Console.WriteLine("Greater than 1000 Numers = " & Greater1000)
Console.ReadKey()
Page 21 of 38
Minimum and Maximum Technique:
DECLARE Temp, Min, Max, Count : INTEGER DECLARE Temp, Min, Max, Count : INTEGER
min = 10000
max = -10000
For Count = 1 To 10
Console.WriteLine("Please enter Temerature")
Temp = Console.ReadLine()
If Temp < min Then
min = Temp
End If
If Temp > max Then
max = Temp
End If
Next
Page 22 of 38
Console.WriteLine(" Minmum Temperature = " & min)
Console.WriteLine(" Maximum Temperature = " & max)
Console.ReadKey()
For Count = 1 To 9
Console.WriteLine("Please enter Temerature")
Temp = Console.ReadLine()
If Temp < Min Then
Min = Temp
End If
If Temp > Max Then
Max = Temp
End If
Next
Console.ReadKey()
Page 23 of 38
Array: 1D ARRAY
Q. Declare an Array with the name of Mars of 10 elements.
Declaration in Pseudo Code:
DECLARE Marks : ARRAY [0:9] OF INTEGER
VB.Net Code:
Dim Marks(9) As Integer
FOR i 0 TO 6
OUTPUT MyList[i]
NEXT
VB.Net Code:
Dim MyList(6) As Integer
Dim i As Integer
For i = 0 To 6
Console.WriteLine("Enter Values")
MyList(i) = Console.ReadLine()
Next
For i = 0 To 6
Console.WriteLine(MyList(i))
Next
Console.ReadKey()
Page 24 of 38
Q. Enter the following values into Array MyList.
VB.Net Code:
Linear / Serial Search
Dim MyList(6) As Integer
Pseudo Code:
MyList(0) = 25
MyList(1) = 34 DECLARE SearchVal , i : INTEGER
MyList(2) = 98
DECLARE Found : BOOLEAN
MyList(3) = 7
MyList(4) = 41 Found False
MyList(5) = 19 i0
MyList(6) = 5 INPUT SearchVal
Console.ReadKey() REPEAT
IF SearchVal = MyList[i] THEN
Found true
ELSE
ii+1
END IF
UNTIL Found = True OR i > 6
IF Found = True THEN
OUTPUT “ Match found at location “ & i
ELSE
OUTPUT “ Match Not Found”
ENDIF
Page 25 of 38
VB.Net Code ( Linear/ Serial Search):
Dim MyList(6) As Integer
MyList(0) = 25
MyList(1) = 34
MyList(2) = 98
MyList(3) = 7
MyList(4) = 41
MyList(5) = 19
MyList(6) = 5
Console.ReadKey()
Page 26 of 38
Bubble Sorting in Ascending order:
DECLARE N ,Count , Temp : INTEGER
DECLARE Swap : BOOLEAN
N 5
REPEAT
Swap False
FOR Count 0 TO N
IF mylist[Count] > Mylist [Count] THEN
Temp MyList[Count]
MyList[Count] MyList[Count]
MyList [Count] Temp
Swap True
ENDIF
NEXT
N N - 1
UNTIL Swap = False
To display sorted Data:
FOR Count= 0 TO 6
OUTPUT MyList[Count]
NEXT
Page 27 of 38
VB.Net Code (Bubble sort in ascending order)
Dim MyList(6) As Integer
MyList(0) = 25
MyList(1) = 34
MyList(2) = 98
MyList(3) = 7
MyList(4) = 41
MyList(5) = 19
MyList(6) = 5
N = 5
Do
Swap = False
For Count = 0 To N
If MyList(Count) > MyList(Count + 1) Then
Temp = MyList(Count)
MyList(Count) = MyList(Count + 1)
MyList(Count + 1) = Temp
Swap = True
End If
Next
N = N - 1
Loop Until Swap = False
Page 28 of 38
2 D Array:
Pseudo Code:
DECLARE BOARD : ARRAY [ 0 : 2 , 0 : 3] OF INTEGER
Enter Data directly by programmer:
Board [0,0] 10
Board [0,1] 20
Board [0,2] 30
Board [0,3] 40
Board [1,0] 50
Board [1,1] 52
Board [1,2] 54
Board [1,3] 56
Board [2,0] 60
Board [2,1] 62
Board [2,2] 64
Board [2,3] 66
Page 29 of 38
VB.Net Code:
Dim Board(2, 3) As Integer ' 2 D Array Declarartion
Board(0, 0) = 10
Board(0, 1) = 20
Board(0, 2) = 30
Board(0, 3) = 40
Board(1, 0) = 50
Board(1, 1) = 52
Board(1, 2) = 54
Board(1, 3) = 56
Board(2, 0) = 60
Board(2, 1) = 62
Board(2, 2) = 64
Board(2, 3) = 66
For i = 0 To 2
For j = 0 To 3
Console.WriteLine("Enter data in 2D Array")
Board(i, j) = Console.ReadLine()
Next
Next
Page 30 of 38
Initialize all elements of 2D Array Grid with ‘A’
Pseudo code:
FOR i 0 TO 2
FOR j 0 TO 2
Grid [I,j] 0
NEXT j
NEXT i
VB.Net Code:
Dim Grid(2, 2) As Char
Dim i, j As Integer
For i = 0 To 2
For j = 0 To 2
Grid(i,j)='A'
Next
Next
Console.ReadKey()
Page 31 of 38
Record Type/ Structure:
TYPE Employee
DECALRE EName : STRING
DECLARE ESal : REAL
DECLARE DOA : DATE
END TYPE
Page 32 of 38
VB.Net Code:
Module Module1
Structure Employee
Dim ENAme As String
Dim ESal As Single
Dim DOA As Date
End Structure
Sub Main()
Dim Emp As Employee
Emp.ENAme = "Muhammad"
Emp.ESal = 50.5
Emp.DOA = #11/11/2000#
Console.ReadKey()
End Sub
End Module
Q. Enter value by user into a record Structure/Type
And output.
DECLARE Emp : Employee
INPUT Emp.EName
INPUT emp.ESal
INPUT Emp.DO
OUTPUT Emp.EName
OUTPUT emp.ESal
OUTPUT Emp.DO
Page 33 of 38
VB.Net Code:
Module Module1
Structure Employee
Dim EName As String
Dim ESal As Single
Dim DOA As Date
End Structure
Sub Main()
Dim Emp As Employee
Console.WriteLine("Enter Employee Name")
Emp.EName = Console.ReadLine()
Console.WriteLine("Enter Employee Salary")
Emp.ESal = Console.ReadLine()
Console.WriteLine("Enter Employee Date of Appointment")
Emp.DOA = Console.ReadLine()
End Sub
End Module
Page 34 of 38
FUNCTION:
Pseudo Code:
Sqr n * n
END FUNCTION
VB.Net Code:
Module Module1
End Module
Page 35 of 38
Q. Make a Function with Identifier Sum
. It takes two values from user
. and returns its Sum.
e.g if user enters 4 and 5 then it returns 9
Pseudo Code:
VB.NET Code:
Module Module1
Function Sum(Num1 As Integer, Num2 As Integer)
Sum = Num1 + Num2
End Function
Sub Main()
Dim Ans As Integer
Ans = Sum(4, 5)
Console.WriteLine("Its Sum = " & Ans) ' Console.WriteLine("Its Sum = " & Ans)
Console.ReadKey()
End Sub
End Module
Page 36 of 38
VB.NET Code: Method 2
Module Module1
Function Sum(Num1 As Integer, Num2 As Integer)
Sum = Num1 + Num2
'Alternative methods
'Dim Add As Integer
' Add = Num1 + Num2
' Return Add / Sum = Add
End Function
Sub Main()
Dim Ans As Integer
Dim N1 As Integer
Dim N2 As Integer
End Module
Procedure:
PROCEDURE Sqr (n : INTEGER) ‘ Procedure Header
DECLARE Ans : INTEGER
Ans n * n
OUTPUT Ans
END PROCEDURE
Page 37 of 38
VB.Net Code:
Module Module1
Sub Main()
Dim Num As Integer
Console.WriteLine("Enter a value to find its square")
Num = Console.ReadLine()
Call Sqr(Num)
Console.ReadKey()
End Sub
End Module
Page 38 of 38