Public Class Class2 Function factorial(ByVal m As Integer) As Integer If m = 0 Then Return 1 Else Return m * factorial(m - 1) End If End Function End Class Module Fact2UsClassNoConst Sub main() Dim n, fact As Integer Dim obj As New Class2 Console.WriteLine("Using NO constructor") Console.WriteLine() Console.Write("Enter a Number : ") n = CInt(Console.ReadLine()) If n < 0 Then Console.WriteLine("Factorial of negative number is NOT possible.") ElseIf n = 0 Or n = 1 Then Console.WriteLine("Factorial of " + n.ToString + " = 1") Else : fact = obj.factorial(n) Console.WriteLine("Factorial of " + n.ToString + " = " + fact.ToString()) End If Console.ReadLine() End Sub End Module
Visual Basic dot net VB.net programming microsoft .net framework object oriented language network enabled technology visual basic 2005 IDE integrated development environment console application microsoft cre common runtime environment
Factorial using class witOUT constructor
Factorial Using Class
Public Class Class1 Dim n, fact As Integer Sub New() Console.Write("Enter a Number : ") n = CInt(Console.ReadLine()) End Sub Sub checking() If n < 0 Then Console.WriteLine("Factorial of negative number is NOT possible.") ElseIf n = 0 Or n = 1 Then Console.WriteLine("Factorial of " + n.ToString + " = 1") Else : fact = factorial(n) Console.WriteLine("Factorial of " + n.ToString + " = " + fact.ToString()) End If End Sub Function factorial(ByVal m As Integer) As Integer If m = 0 Then Return 1 Else Return m * factorial(m - 1) End If End Function End Class Module FactUsClass Sub Main() Dim obj As New Class1 obj.checking() Console.ReadLine() End Sub End Module
Find Sum using Class
Public Class Class1 Dim a, b As Integer Sub New() Console.Write("Enter 1st number : ") a = CInt(Console.ReadLine()) Console.Write("Enter 2nd number : ") b = CInt(Console.ReadLine()) End Sub Sub add() Dim c As Integer c = a + b Console.WriteLine() Console.WriteLine("Sum = " + c.ToString()) End Sub End Class Module SumUsingClass Sub Main() Dim obj As New Class1 obj.add() Console.ReadLine() End Sub End Module
Simple Display Using Class
Public Class class1 Dim n As Integer Sub New() Console.WriteLine("Inside Constructor...") Console.Write("Enter the value of n : ") n = CInt(Console.ReadLine()) End Sub Sub display() Console.WriteLine("n= " + n.ToString()) End Sub End Class Module DisplayUsingClass Sub Main() Dim obj As New class1 obj.display() Console.ReadLine() End Sub End Module
Find GCD using recursion
Module GCDRecFunc Sub Main() Dim m, n, g As Integer Console.Write("Enter 1st Number : ") m = CInt(Console.ReadLine()) Console.Write("Enter 2nd Number : ") n = CInt(Console.ReadLine()) g = gcd(m, n) Console.WriteLine("GCD = " + g.ToString()) Console.ReadLine() End Sub Function gcd(ByVal x As Integer, ByVal y As Integer) As Integer If x Mod y = 0 Then Return y Else Return gcd(y, x Mod y) End If End Function End Module
Factorial of a number using Recursion
Module FactorialRecFunc Sub Main() Dim n, fact As Integer Console.Write("Enter Number : ") n = CInt(Console.ReadLine()) If n < 0 Then Console.WriteLine("Factorial of negative number is NOT possible.") ElseIf n = 0 Or n = 1 Then Console.WriteLine("Factorial of " + n.ToString() + " =1") Else fact = factorial(n) Console.WriteLine("Factorial of " + n.ToString() + " =" + fact.ToString()) End If Console.ReadLine() End Sub Function factorial(ByVal m As Integer) As Integer If m = 0 Then Return 1 Else Return m * factorial(m - 1) End If End Function End Module
Linear Search Using Function
Module LinSearchFunc Sub Main() Console.WriteLine("Linear Searching using Function") Console.WriteLine() Dim n, i, k, item As Integer Console.Write("Enter Number of Elements: ") n = CInt(Console.ReadLine) Dim arr(n) As Integer Console.WriteLine() For i = 0 To n - 1 Console.Write("Enter Element(" & (i + 1) & "): ") arr(i) = CInt(Console.ReadLine) Next Console.WriteLine() Console.WriteLine("Before Sorting") Console.WriteLine() For i = 0 To n - 1 Console.WriteLine("Element in (" & i & "): " & arr(i)) Next Console.WriteLine() Console.Write("Enter the Element to be searched:") item = CInt(Console.ReadLine()) k = linear_search(arr, n, item) Console.WriteLine() If k <> -1 Then Console.WriteLine("Element is Found") Else Console.WriteLine("Element is Not Found ") End If Console.ReadLine() End Sub function linear_search(byval x() as integer,byval y as integer,byval z as integer) Dim j As Integer For j = 0 To y - 1 If x(j) = z Then Return j End If Next Return -1 End Function End Module
ByVal Calling
Module Module1 Sub Main() Dim result As Integer Dim x, y As Integer Console.WriteLine("ByVal") Console.Write("Enter Original value1:") x = CInt(Console.ReadLine) Console.Write("Enter Original value2:") y = CInt(Console.ReadLine) Console.WriteLine() result = sum2values(x, y) 'calling the function with 2 parameters Console.WriteLine("Result=" & result) Console.WriteLine() Console.WriteLine("Now Values of x=" & x & " and y=" & y) Console.WriteLine() Console.WriteLine("x and y are Unchanged") Console.ReadLine() End Sub 'defining a function with 2 parameters Function sum2values(ByVal x, ByVal y) 'modifying values of x and y Console.Write("Enter New value1:") x = CInt(Console.ReadLine) Console.Write("Enter New value2:") y = CInt(Console.ReadLine) sum2values = x + y End Function End Module
ByRef Calling
Module Module2 Sub Main() Dim result As Integer Dim x, y As Integer Console.WriteLine("ByRef") Console.Write("Enter Original value1:") x = CInt(Console.ReadLine) Console.Write("Enter Original value2:") y = CInt(Console.ReadLine) Console.WriteLine() result = sum2values(x, y) 'calling the function with 2 parameters Console.WriteLine("Result=" & result) Console.WriteLine() Console.WriteLine("Now Values of x=" & x & " and y=" & y) Console.WriteLine() Console.WriteLine("x and y are changed") Console.ReadLine() End Sub 'defining a function with 2 parameters Function sum2values(ByRef x, ByRef y) 'modifying values of x and y Console.Write("Enter New value1:") x = CInt(Console.ReadLine) Console.Write("Enter New value2:") y = CInt(Console.ReadLine) sum2values = x + y End Function End Module
Bubble Sorting Using Function
Module BSFun Sub Main() Console.WriteLine("Bubble Sorting using Function") Console.WriteLine() Dim n, i As Integer Console.Write("Enter Number of Elements: ") n = CInt(Console.ReadLine) Dim arr(n) As Integer Console.WriteLine() For i = 0 To n - 1 Console.Write("Enter Element(" & (i + 1) & "): ") arr(i) = CInt(Console.ReadLine) Next Console.WriteLine() Console.WriteLine("Before Sorting") Console.WriteLine() For i = 0 To n - 1 Console.WriteLine("Element in (" & i & "): " & arr(i)) Next sort_array(arr, n) Console.WriteLine("Sorted Array") Console.WriteLine() For i = 0 To n - 1 Console.WriteLine("Element in (" & i & "): " & arr(i)) Next Console.ReadLine() End Sub Sub sort_array(ByVal x() As Integer, ByVal y As Integer) Dim i, j, t As Integer For i = 0 To y - 1 For j = i + 1 To y - 1 If x(i) > x(j) Then t = x(i) x(i) = x(j) x(j) = t End If Next Next End Sub End Module
Summation of elements of an array Using Function
Module SumArFunc Sub Main() Dim n, i, s As Integer Console.Write("Enter Number of elements: ") n = CInt(Console.ReadLine()) Dim arr(n) As Integer Console.WriteLine() For i = 0 To n - 1 Console.Write("Enter Element (" + (i + 1).ToString() + ")= ") arr(i) = CInt(Console.ReadLine()) Next Console.WriteLine() Console.WriteLine("Array") Console.WriteLine("-------------") For i = 0 To n - 1 Console.WriteLine("Array (" & i & ")= " & arr(i)) Next Console.WriteLine("-------------") s = sum(arr, n) Console.WriteLine("Sum of Elements= " & s.ToString()) Console.ReadLine() End Sub Function sum(ByVal x() As Integer, ByVal y As Integer) Dim j, s1 As Integer s1 = 0 For j = 0 To y - 1 s1 += x(j) Next Return s1 End Function End Module
Swapping of 2 integers Using Function
Module SwapByFunction Sub Main() Dim a, b As Integer swap(a, b) Console.ReadLine() End Sub Sub swap(ByVal a, ByVal b) Dim t As Integer Console.Write("a = ") a = CInt(Console.ReadLine()) Console.Write("b = ") b = CInt(Console.ReadLine()) Console.WriteLine() Console.WriteLine("Before Swapping...") Console.WriteLine("a = " + a.ToString() + " b = " + b.ToString()) t = a a = b b = t Console.WriteLine() Console.WriteLine("After Swapping...") Console.WriteLine("a = " + a.ToString() + " b = " + b.ToString()) End Sub End Module
Summation Using Function 3
Module Module2 Sub Main() Dim result As Integer Dim x, y As Integer Console.Write("Enter value1:") x = CInt(Console.ReadLine) Console.Write("Enter value2:") y = CInt(Console.ReadLine) result = sum2values(x, y) 'calling the function with 2 parameters Console.WriteLine("Result=" & result) Console.ReadLine() End Sub 'defining a function with 2 parameters Function sum2values(ByVal x, ByVal y) sum2values = x + y End Function End Module
Summation Using Function 2
Module Module1 Sub Main() Dim result As Integer result = sum2values() 'calling the function Console.WriteLine("Result=" & result) Console.ReadLine() End Sub 'defining a function Function sum2values() Dim i, j As Integer Console.Write("Enter 1st Number: ") i = CInt(Console.ReadLine()) Console.Write("Enter 2nd Number: ") j = CInt(Console.ReadLine()) sum2values = i + j End Function End Module
Summation Using Function 1
Module SumUsingFunction Sub Main() Dim i, j, s As Integer Console.Write("Enter 1st Number: ") i = CInt(Console.ReadLine()) Console.Write("Enter 2nd Number: ") j = CInt(Console.ReadLine()) s = sum(i, j) Console.WriteLine("Sum of " + i.ToString() + " + " + j.ToString() + " = " + s.ToString()) Console.ReadLine() End Sub Function sum(ByVal a As Integer, ByVal b As Integer) As Integer sum = a + b End Function End Module
A short Project on All String Function
Module StringFunctions Sub main() Dim strng As String Dim key, nu, ascii As String key = 89 While key = 89 Or key = 121 Console.Clear() Console.WriteLine("---------------------") Console.WriteLine("---String Function---") Console.WriteLine("---------------------") Console.WriteLine("1.Asc()") Console.WriteLine("2.Chr()") Console.WriteLine("3.GetChar") Console.WriteLine("4.Instr()") Console.WriteLine("5.InStrRev") Console.WriteLine("6.LCase") Console.WriteLine("7.Left()") Console.WriteLine("8.LTrim") Console.WriteLine("9.Mid()") Console.WriteLine("10.Replace()") Console.WriteLine("11.Right()") Console.WriteLine("12.Rtrim()") Console.WriteLine("13.Str()") Console.WriteLine("14.Space()") Console.WriteLine("15.StrComp()") Console.WriteLine("16.StrRev()") Console.WriteLine("17.Trim()") Console.WriteLine("18.Ucase()") Dim x As Integer Console.WriteLine() Console.Write("Please enter your choice: ") x = CInt(Console.ReadLine()) Console.WriteLine() If x <= 18 Then Select Case x Case 1 Console.Write("Enter a String: ") strng = Console.ReadLine() Dim Ascii_val As String Ascii_val = Asc(strng) Console.WriteLine("Ascii value=" + Ascii_val.ToString()) Console.WriteLine() Case 2 Dim n As Integer Console.Write("Enter the ascii value: ") n = CInt(Console.ReadLine()) Dim char_code As String char_code = Chr(n) Console.WriteLine("char code=" & char_code) Console.WriteLine() Case 3 Console.Write("Enter a String: ") strng = Console.ReadLine() Dim get_char As String Dim n1 As Integer Console.Write("Enter the position: ") n1 = CInt(Console.ReadLine()) get_char = GetChar(strng, n1) Console.WriteLine("get char=" & get_char) Console.WriteLine() Case 4 Console.Write("Enter a String: ") strng = Console.ReadLine() Dim sub_str As String Console.Write("Enter the sub string: ") sub_str = Console.ReadLine() Dim pos As Integer = InStr(strng, sub_str) Console.WriteLine("Starting positon=" & pos) Console.WriteLine() Case 5 Console.Write("Enter a String: ") strng = Console.ReadLine() Dim sub_str2 As String Console.Write("Enter the sub string: ") sub_str2 = Console.ReadLine() Dim pos2 As Integer = InStrRev(strng, sub_str2) Console.WriteLine("Starting positon=" & pos2) Console.WriteLine() Case 6 Console.Write("Enter a String in UPPER CASE: ") strng = Console.ReadLine() Dim low_case As String = LCase(strng) Console.WriteLine("string in lower case=" & low_case) Console.WriteLine() Case 7 Console.Write("Enter a String: ") strng = Console.ReadLine() Dim n4 As Integer Console.Write("Enter number of characters: ") n4 = CInt(Console.ReadLine()) Dim sub_str4 As String = Left(strng, n4) Console.WriteLine("Sub String: " + sub_str4) Console.WriteLine() Case 8 Console.Write("Enter a String starting with spaces: ") strng = Console.ReadLine() Dim left_trim As String = LTrim(strng) Console.WriteLine("After left Trimming=" & left_trim) Console.WriteLine() Case 9 Console.Write("Enter a String: ") strng = Console.ReadLine() Dim n2, n3 As Integer Console.Write("Enter starting position: ") n2 = CInt(Console.ReadLine()) Console.Write("Enter number of characters: ") n3 = CInt(Console.ReadLine()) Dim sub_str3 As String = Mid(strng, n2, n3) Console.WriteLine("Sub String: " & sub_str3) Console.WriteLine() Case 10 Console.Write("Enter a String: ") strng = Console.ReadLine() Dim c As String Console.Write("enter character: ") c = Console.ReadLine() Dim sub_str5 As String Console.Write("enter new string: ") sub_str5 = Console.ReadLine() Dim new_str As String = Replace(strng, c, sub_str5) Console.WriteLine("New String: " + new_str) Console.WriteLine() Case 11 Console.Write("Enter a String: ") strng = Console.ReadLine() Dim n4 As Integer Console.Write("Enter starting position: ") n4 = CInt(Console.ReadLine()) Dim sub_str4 As String = Right(strng, n4) Console.WriteLine("Sub String: " + sub_str4) Console.WriteLine() Case 12 Console.Write("Enter a String with spaces at end: ") strng = Console.ReadLine() Dim right_trim As String = RTrim(strng) Console.WriteLine("After right Trimming=" & right_trim) Console.WriteLine() Case 13 Dim n As Integer Console.Write("Enter your number: ") n = CInt(Console.ReadLine()) Dim sub_str As String = Str(n) Console.WriteLine("String equivalent: " + sub_str) Console.WriteLine() Case 14 Console.Write("Enter a String: ") strng = Console.ReadLine() Dim n As Integer Console.Write("Enter number of spaces: ") n = CInt(Console.ReadLine()) Dim new_str As String = (strng & Space(n) & strng) Console.WriteLine("new_str: " + new_str) Console.WriteLine() Case 15 Console.Write("Enter 1st String: ") strng = Console.ReadLine() Dim new_str As String Console.Write("Enter 2nd String to compare: ") new_str = Console.ReadLine() Dim comp As String = StrComp(strng, new_str) If comp = 0 Then Console.WriteLine("Both strings are equal.") ElseIf comp = 1 Then Console.WriteLine("First string has greater value.") Else Console.WriteLine("Second string has greater value.") End If Console.WriteLine() Case 16 Console.Write("Enter a String: ") strng = Console.ReadLine() Dim str_rev As String = StrReverse(strng) Console.WriteLine("String in reverse: " + str_rev) Console.WriteLine() Case 17 Console.Write("Enter a String with spaces on both sides: ") strng = Console.ReadLine() Dim trimming As String = Trim(strng) Console.WriteLine("After Trimming=" & trimming) Console.WriteLine() Case 18 Console.Write("Enter a String in lower case: ") strng = Console.ReadLine() Dim up_case As String = UCase(strng) Console.WriteLine("string in UPPER case=" & up_case) Console.WriteLine() End Select Else Console.WriteLine("Invalid Number") Console.WriteLine() End If Console.Write("Do you want to continue(Y/N): ") nu = Console.ReadLine() ascii = Asc(nu) key = ascii End While End Sub End Module
Subscribe to:
Posts (Atom)