0% found this document useful (0 votes)
10 views38 pages

Basic Programming Tasks in Visual Basic

The document contains a series of programming assignments that require writing various computer programs in Visual Basic. Each assignment includes tasks such as calculating the square of a number, finding the maximum of three numbers, sorting numbers, checking if a number is even, swapping values, and performing calculations related to geometry and salary. The assignments are structured for both normal and function-based approaches.
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)
10 views38 pages

Basic Programming Tasks in Visual Basic

The document contains a series of programming assignments that require writing various computer programs in Visual Basic. Each assignment includes tasks such as calculating the square of a number, finding the maximum of three numbers, sorting numbers, checking if a number is even, swapping values, and performing calculations related to geometry and salary. The assignments are structured for both normal and function-based approaches.
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

INDIVIDUAL ASSIGNMENT No: 01(NORMAL)

1. Write a computer program that accept a number and output the square of the input
number.
ModuleModule1
SubMain()
Dim number AsDouble
Dim square AsDouble

[Link]("Enter a number: ")


number = [Link]()
square = number * number
[Link]("The square of the number is: "& square)
[Link]()
EndSub
EndModule

2. Write the computer program that ask user to enter three numbers and the program
should find the maximum of the three number and display the maximum number.
ModuleModule1
SubMain()
Dim num1, num2, num3, maxNumAsInteger
[Link]("Enter the first number: ")
num1 = [Link]()
[Link]("Enter the second number: ")
num2 = [Link]()
[Link]("Enter the third number: ")
num3 = [Link]()
maxNum = num1
If num2 >maxNumThen
maxNum = num2
EndIf
If num3 >maxNumThen
maxNum = num3
EndIf
[Link]("The maximum number is: "&maxNum)
[Link]()
EndSub
EndModule
3. Write the computer program that ask user to enter three numbers and the program
should sort the numbers entered by the user in ascending order. The program then output
the three numbers entered by the user sorted in ascending order.
ModuleModule1
SubMain()
Dim num1, num2, num3, temp AsInteger
[Link]("Enter the first number: ")
num1 = [Link]()
[Link]("Enter the second number: ")
num2 = [Link]()
[Link]("Enter the third number: ")
num3 = [Link]()
' Sort the numbers
If num1 > num2 Then
temp = num1
num1 = num2
num2 = temp
EndIf
If num2 > num3 Then
temp = num2
num2 = num3
num3 = temp
EndIf
If num1 > num2 Then
temp = num1
num1 = num2
num2 = temp
EndIf
[Link]("The numbers in ascending order are: "& num1 &", "& num2 &", "& num3)
[Link]()
EndSub
EndModule

4. Write a computer program that ask the user to enter any number and then the program
display the output as True if a number entered by the user is even and False if a number is
odd.
ModuleModule1
SubMain()
DimnumAsInteger
[Link]("Enter a number: ")
num = [Link]()
IfnumMod 2 = 0 Then
[Link]("True")
Else
[Link]("False")
EndIf
[Link]()

EndSub
EndModule

5. Write a computer program that accept two numbers from user. Store the entered
numbers in variables named Value1 and Value2. The program should replace the content
of Value1 with the content of Value2 and also replace the content of Value2 with the
content of Value1, this process is known as swapping. The program should display the
content of variables Value1 and Value2 after swapping.
ModuleModule1
SubMain()
Dim Value1, Value2, temp AsInteger
[Link]("Enter the first number: ")
Value1 = [Link]()
[Link]("Enter the second number: ")
Value2 = [Link]()
' Swapping the values
temp = Value1
Value1 = Value2
Value2 = temp
[Link]("After swapping, the content of Value1 is: "& Value1)
[Link]("After swapping, the content of Value2 is: "& Value2)
[Link]()
EndSub
EndModule
6. Write a program that accept five digit number and store it in a variable num and the
program shall find the sum of its digits. So, if the input is like num = 58612, then the output
will be 22 because 5 + 8 + 6 + 1 + 2 = 22.
ModuleModule1
SubMain()
Dimnum, sum, digit AsInteger
[Link]("Enter a five-digit number: ")
num = [Link]()
sum = 0
Whilenum<> 0
digit = numMod 10
sum = sum + digit
num = num \ 10
EndWhile
[Link]("The sum of the digits is: "& sum)
[Link]()
EndSub
EndModule
7. Write a program that accept five digit number and store it in a variable num and the
program shall reverse its digits. So, if the input is like num = 58612, then the output will be
21685.
ModuleModule1
SubMain()
Dimnum, reversedNum, digit AsInteger
[Link]("Enter a five-digit number: ")
num = [Link]()
reversedNum = 0
Whilenum<> 0
digit = numMod 10
reversedNum = reversedNum * 10 + digit
num = num \ 10
EndWhile
[Link]("The reversed number is: "&reversedNum)
[Link]()
EndSub
EndModule

8. The length & breadth of a rectangle and radius of a circle are input through the
keyboard. Write a program to calculate the area & perimeter of the rectangle, and the area
& circumference of the circle. (Where area of a triangle is length * breadth, perimeter is 2*
(length * breadth), area of a circle is 3.14*r*r and circumference of the circle is 2*3.14*r)
ModuleModule1
SubMain()
Dim length, breadth, radius AsDouble
DimareaRectangle, perimeterRectangle, areaCircle, circumferenceCircleAsDouble

[Link]("Enter the length of the rectangle: ")


length = [Link]()
[Link]("Enter the breadth of the rectangle: ")
breadth = [Link]()
[Link]("Enter the radius of the circle: ")
radius = [Link]()
areaRectangle = length * breadth
perimeterRectangle = 2 * (length + breadth)
areaCircle = 3.14 * radius * radius
circumferenceCircle = 2 * 3.14 * radius
[Link]("The area of the rectangle is: "&areaRectangle)
[Link]("The perimeter of the rectangle is: "&perimeterRectangle)
[Link]("The area of the circle is: "&areaCircle)
[Link]("The circumference of the circle is: "&circumferenceCircle)
[Link]()
End sub
End module
9. Ramesh’s basic salary is input through the keyboard. His dearness allowance is 40% of
basic salary, and house rent allowance is 20% of basic salary. Write a program to calculate
and display his gross salary. Where gross salary = basic salary + dearness allowance +
house rent allowance
ModuleModule1
SubMain()
DimbasicSalaryAsDouble
DimdearnessAllowanceAsDouble
DimhouseRentAllowanceAsDouble
DimgrossSalaryAsDouble
[Link]("Enter the basic salary: ")
basicSalary = [Link]([Link]())
dearnessAllowance = 0.4 * basicSalary
houseRentAllowance = 0.2 * basicSalary
grossSalary = basicSalary + dearnessAllowance + houseRentAllowance
[Link]("The gross salary is: "&grossSalary)
[Link]()
EndSub
End module

[Link] a program that ask user to enter any number and the program should output the
message “You have entered even number” if a number entered by the user is even and the
program should output the message “The number entered is not even number” if the
number entered by the user is odd.
ModuleModule1
SubMain()
Dim number AsInteger
[Link]("Enter any number: ")
number = [Link]()
If number Mod 2 = 0 Then
[Link]("You have entered an even number.")
Else
[Link]("The number entered is not an even number.")
EndIf
[Link]()
EndSub
EndModule

11. The distance between two cities (in km.) is input through the keyboard. Write a
program to convert and print this distance in meters, feet, inches and centimeters. (Note:
1km = 1000m, 1m=100cm, 1cm=2.54inches and 1inch=12feet)
ModuleModule1
SubMain()
DimdistanceKmAsDouble
DimdistanceMetersAsDouble
DimdistanceFeetAsDouble
DimdistanceInchesAsDouble
DimdistanceCmAsDouble

[Link]("Enter the distance between two cities in km: ")


distanceKm = [Link]()
distanceMeters = distanceKm * 1000
distanceCm = distanceMeters * 100
distanceInches = distanceCm / 2.54
distanceFeet = distanceInches / 12

[Link]("The distance in meters is: "&distanceMeters)


[Link]("The distance in feet is: "&distanceFeet)
[Link]("The distance in inches is: "&distanceInches)
[Link]("The distance in centimeters is: "&distanceCm)
[Link]()
EndSub
EndModule

12. If the marks obtained by a student in five different subjects are input through the
keyboard, find out the average marks obtained by the student. Assume that the maximum
marks that can be obtained by a student in each subject is 100.
ModuleModule1
SubMain()
Dim marks1 AsDouble
Dim marks2 AsDouble
Dim marks3 AsDouble
Dim marks4 AsDouble
Dim marks5 AsDouble
DimaverageMarksAsDouble
[Link]("Enter the marks for subject 1: ")
marks1 = [Link]()
[Link]("Enter the marks for subject 2: ")
marks2 = [Link]()
[Link]("Enter the marks for subject 3: ")
marks3 = [Link]()
[Link]("Enter the marks for subject 4: ")
marks4 = [Link]()
[Link]("Enter the marks for subject 5: ")
marks5 = [Link]()
averageMarks = (marks1 + marks2 + marks3 + marks4 + marks5) / 5
[Link]("The average marks obtained by the student is: "&averageMarks)
[Link]()
EndSub
EndModule
INDIVIDUAL ASSIGNMENT No: 01(BY FUNCTION)
1. Write a computer program that accept a number and output the square of the input
number.
ModuleModule1
Module Module1
Sub Main()
[Link]("Enter a number: ")
Dim num As Double = [Link]()
[Link]("The square of the number is: " & Square(num))
[Link]()
End Sub
Function Square(n As Double) As Double
Return n * n
End Function
End Module

2. Write the computer program that ask user to enter three numbers and the program
should find the maximum of the three number and display the maximum number.
ModuleModule1
Module Module1
Sub Main()
[Link]("Enter the first number: ")
Dim num1 As Double = [Link]()
[Link]("Enter the second number: ")
Dim num2 As Double = [Link]()
[Link]("Enter the third number: ")
Dim num3 As Double = [Link]()
Dim maxNum As Double = Max(num1, num2, num3)
[Link]("The maximum number is: " &maxNum)
[Link]()
End Sub
Function Max(a As Double, b As Double, c As Double) As Double
If a >= b AndAlso a >= c Then
Return a
ElseIf b >= a AndAlso b >= c Then
Return b
Else
Return c
End If
End Function
End Module
EndModule
3. Write the computer program that ask user to enter three numbers and the program
should sort the numbers entered by the user in ascending order. The program then output
the three numbers entered by the user sorted in ascending order.
ModuleModule1
Module Module1
Sub Main()
[Link]("Enter the first number: ")
Dim num1 As Double = [Link]()
[Link]("Enter the second number: ")
Dim num2 As Double = [Link]()
[Link]("Enter the third number: ")
Dim num3 As Double = [Link]()

Dim sortedNums As Tuple(Of Double, Double, Double) = SortNumbers(num1, num2,


num3)
[Link]("The numbers in ascending order are: " & sortedNums.Item1 & ", " &
sortedNums.Item2 & ", " & sortedNums.Item3)
[Link]()
End Sub
Function SortNumbers(a As Double, b As Double, c As Double) As Tuple(Of Double,
Double, Double)
Dim min As Double = [Link]([Link](a, b), c)
Dim max As Double = [Link]([Link](a, b), c)
Dim mid As Double = a + b + c - min - max
Return [Link](min, mid, max)
End Function
End Module
EndModule

4. Write a computer program that ask the user to enter any number and then the program
display the output as True if a number entered by the user is even and False if a number is
odd.
ModuleModule1
Module Module1
Sub Main()
[Link]("Enter a number: ")
Dim num As Integer = [Link]()
[Link]("Is the number even? " &IsEven(num))
[Link]()
End Sub
Function IsEven(n As Integer) As Boolean
Return n Mod 2 = 0
End Function
End Module
EndModule

5. Write a computer program that accept two numbers from user. Store the entered
numbers in variables named Value1 and Value2. The program should replace the content
of Value1 with the content of Value2 and also replace the content of Value2 with the
content of Value1, this process is known as swapping. The program should display the
content of variables Value1 and Value2 after swapping.
ModuleModule1
SubMain()
[Link]("Enter the first number: ")
Dim Value1 AsInteger = [Link]([Link]())
[Link]("Enter the second number: ")
Dim Value2 AsInteger = [Link]([Link]())
[Link]("Before swapping: Value1 = "& Value1 &", Value2 = "& Value2)
SwapValues(Value1, Value2)
[Link]("After swapping: Value1 = "& Value1 &", Value2 = "& Value2)
[Link]()
EndSub
FunctionSwapValues(ByRef Value1 AsInteger, ByRef Value2 AsInteger)
Dim temp AsInteger = Value1
Value1 = Value2
Value2 = temp
EndFunction
EndModule
6. Write a program that accept five digit number and store it in a variable num and the
program shall find the sum of its digits. So, if the input is like num = 58612, then the output
will be 22 because 5 + 8 + 6 + 1 + 2 = 22.
ModuleModule1
Module Module1
Sub Main()
[Link]("Enter a five digit number: ")
Dim num As Integer = [Link]([Link]())
[Link]("The sum of the digits is: " &SumOfDigits(num))
[Link]()
End Sub
Function SumOfDigits(ByVal num As Integer) As Integer
Dim sum As Integer = 0
While num <> 0
sum += num Mod 10
num \= 10
End While
Return sum
End Function
End Module

7. Write a program that accept five digit number and store it in a variable num and the
program shall reverse its digits. So, if the input is like num = 58612, then the output will be
21685.
ModuleModule1
Module Module1
Sub Main()
[Link]("Enter a five digit number: ")
Dim num As Integer = [Link]([Link]())
[Link]("The reversed number is: " &ReverseDigits(num))
[Link]()
End Sub
Function ReverseDigits(ByVal num As Integer) As Integer
Dim reverse As Integer = 0
While num <> 0
reverse = reverse * 10 + num Mod 10
num \= 10
End While
Return reverse
End Function
End Module

8. The length & breadth of a rectangle and radius of a circle are input through the
keyboard. Write a program to calculate the area & perimeter of the rectangle, and the area
& circumference of the circle. (Where area of a triangle is length * breadth, perimeter is 2*
(length * breadth), area of a circle is 3.14*r*r and circumference of the circle is 2*3.14*r)
ModuleModule1
Module Module1
Sub Main()
[Link]("Enter length of the rectangle: ")
Dim length As Double = [Link]()

[Link]("Enter breadth of the rectangle: ")


Dim breadth As Double = [Link]()

[Link]("Enter radius of the circle: ")


Dim radius As Double = [Link]()
[Link]("Area of rectangle: " &AreaRectangle(length, breadth))
[Link]("Perimeter of rectangle: " &PerimeterRectangle(length, breadth))
[Link]("Area of circle: " &AreaCircle(radius))
[Link]("Circumference of circle: " &CircumferenceCircle(radius))
[Link]()
End Sub
Function AreaRectangle(ByVal length As Double, ByVal breadth As Double) As Double
Return length * breadth
End Function
Function PerimeterRectangle(ByVal length As Double, ByVal breadth As Double) As Double
Return 2 * (length + breadth)
End Function
Function AreaCircle(ByVal radius As Double) As Double
Return 3.14 * radius * radius
End Function
Function CircumferenceCircle(ByVal radius As Double) As Double
Return 2 * 3.14 * radius
End Function
End Module
End module

9. Ramesh’s basic salary is input through the keyboard. His dearness allowance is 40% of
basic salary, and house rent allowance is 20% of basic salary. Write a program to calculate
and display his gross salary. Where gross salary = basic salary + dearness allowance +
house rent allowance
ModuleModule1
Module Module1
Sub Main()
[Link]("Enter Ramesh's basic salary: ")
Dim basicSalary As Double = [Link]()
Dim grossSalary As Double = CalculateGrossSalary(basicSalary)
[Link]("Ramesh's gross salary is: " &grossSalary)
[Link]()
End Sub
Function CalculateGrossSalary(ByValbasicSalary As Double) As Double
Dim dearnessAllowance As Double = 0.4 * basicSalary
Dim houseRentAllowance As Double = 0.2 * basicSalary
Return basicSalary + dearnessAllowance + houseRentAllowance
End Function
End Module
End module

[Link] a program that ask user to enter any number and the program should output the
message “You have entered even number” if a number entered by the user is even and the
program should output the message “The number entered is not even number” if the
number entered by the user is odd.
ModuleModule1
Module Module1
Sub Main()
[Link]("Enter a number: ")
Dim number As Integer = [Link]()
If IsEven(number) Then
[Link]("You have entered an even number.")
Else
[Link]("The number entered is not an even number.")
End If
[Link]()
End Sub
Function IsEven(ByVal number As Integer) As Boolean
Return number Mod 2 = 0
End Function
End Module
EndModule

11. The distance between two cities (in km.) is input through the keyboard. Write a
program to convert and print this distance in meters, feet, inches and centimeters. (Note:
1km = 1000m, 1m=100cm, 1cm=2.54inches and 1inch=12feet)\
ModuleModule1
Module Module1
Sub Main()
[Link]("Enter the distance between two cities in km: ")
Dim distanceInKm As Double = [Link]()
[Link]("Distance in meters: " &ConvertKmToMeters(distanceInKm))
[Link]("Distance in feet: " &ConvertKmToFeet(distanceInKm))
[Link]("Distance in inches: " &ConvertKmToInches(distanceInKm))
[Link]("Distance in centimeters: " &ConvertKmToCm(distanceInKm))
[Link]()
End Sub
Function ConvertKmToMeters(ByVal km As Double) As Double
Return km * 1000
End Function
Function ConvertKmToFeet(ByVal km As Double) As Double
Return km * 1000 * 100 * 2.54 * 12
End Function
Function ConvertKmToInches(ByVal km As Double) As Double
Return km * 1000 * 100 * 2.54
End Function
Function ConvertKmToCm(ByVal km As Double) As Double
Return km * 1000 * 100
End Function
End Module
EndSub
EndModule

12. If the marks obtained by a student in five different subjects are input through the
keyboard, find out the average marks obtained by the student. Assume that the maximum
marks that can be obtained by a student in each subject is 100.
ModuleModule1
Module Module1
Sub Main()
[Link]("Enter marks for subject 1: ")
Dim marks1 As Double = [Link]()
[Link]("Enter marks for subject 2: ")
Dim marks2 As Double = [Link]()
[Link]("Enter marks for subject 3: ")
Dim marks3 As Double = [Link]()

[Link]("Enter marks for subject 4: ")


Dim marks4 As Double = [Link]()
[Link]("Enter marks for subject 5: ")
Dim marks5 As Double = [Link]()
Dim averageMarks As Double = CalculateAverage(marks1, marks2, marks3, marks4,
marks5)
[Link]("The average marks obtained by the student is: " &averageMarks)
[Link]()
End Sub
Function CalculateAverage(ByVal marks1 As Double, ByVal marks2 As Double, ByVal
marks3 As Double, ByVal marks4 As Double, ByVal marks5 As Double) As Double
Return (marks1 + marks2 + marks3 + marks4 + marks5) / 5
End Function
End Module
EndSub
EndModule

TASK:ASSIGNMENT NO:02(BY NORMAL)

[Link] a program that prompts the user for an integer and display if the provided integer
is prime number or not a prime number is a number that is divisible only by1 and itself.
First few number are 2,3,5,7,11,13 and so on.
ModuleModule1
Sub Main()
[Link]("Enter an integer: ")
Dim num AsInteger
num = [Link]()
Dim isPrime AsBoolean = True
If num <= 1 Then
isPrime = False
Else
For i AsInteger = 2 To num - 1
If num Mod i = 0 Then
isPrime = False
Exit For
EndIf
Next
EndIf
If isPrime Then
[Link](num &" is a prime number.")
Else
[Link](num &" is not a prime number.")
EndIf
[Link]()
EndSub
EndModule

2. Write a program that prints the first 30 values in the Fibonacci series. A Fibonacci series
begins with 0 and 1. The next number is then found by adding the previous two numbers.
The first few numbers in the Fibonacci series are: 0,1,1,2,3,5,8,13 and so on.
ModuleModule1
Sub Main()
Dim n AsInteger = 30
Dim first AsInteger = 0
Dim second AsInteger = 1
[Link]("First "& n &" values in the Fibonacci series:")
For i AsInteger = 1 To n
[Link](first &" ")
Dim temp AsInteger = first + second
first = second
second = temp
Next i
[Link]()
EndSub
EndModule

3. Write a program that prompts the user for an integer value. The program should then
calculate and print the factorial of the user provided value. Factorial of a number, n,
written as n! is calculated as a product of all integers less than or equal to n. 5! = 5*4*3*2*1
= 120. 0! = 1. 1! = 1.
ModuleModule1
Sub Main()
Dim num AsInteger
[Link]("Enter a number: ")
num = [Link]()
[Link]("The factorial of "& num &" is "& Factorial(num))
[Link]()
EndSub
Function Factorial(ByVal n AsInteger) AsInteger
If n = 0 Or n = 1 Then
Return 1
Else
Return n * Factorial(n - 1)
EndIf
EndFunction
EndModule

4. Write a program that accepts an integer (of any number of digits) from the user and
displays the sum of the digits of the provided integer. Sample output for value 235: Sum of
digits of 235 is 10.
Module Module1
Sub Main()
[Link]("Enter a number: ")
Dim num As Integer = [Link]()
[Link]("Sum of digits of " & num & " is " & SumOfDigits(num))
End Sub
Function SumOfDigits(ByVal num As Integer) As Integer
Dim sum As Integer = 0
While num <> 0
sum += num Mod 10
num \= 10
End While
Return sum
End Function
End Module

5. Write a program that accepts a String value from the user and displays the reverse of
that value.
Sample output for value "Hello, World!":
Reverse of "Hello, World!" is "!dlroW ,olleH"
ModuleModule1
Sub Main()
[Link]("Enter a string: ")
Dim str AsString = [Link]()
[Link]("Reverse of the word is "& ReverseString(str))
[Link]()
EndSub
Function ReverseString(ByVal str AsString) AsString
Dim revStr AsString
For i AsInteger = [Link] - 1 To 0 Step -1
revStr &= str(i)
Next
Return revStr
EndFunction
EndModule

For additional challenge, determine if the String and its reverse are equal and display a message
explaining the result.
Sample output for value "Hello, World!":
String
ModuleModule1
Sub Main()
[Link]("Enter a string:")
Dim userInput AsString = [Link]()
Dim reversedString AsString = ReverseString(userInput)
[Link]("Reverse of the string: "& reversedString)
If [Link](reversedString) Then
[Link]("The original string is equal to its reverse.")
Else
[Link]("The original string is not equal to its reverse.")
EndIf
[Link]()
EndSub
Function ReverseString(inputString AsString) AsString
Dim reversedString AsString = ""
For i AsInteger = [Link] - 1 To 0 Step -1
reversedString &= inputString(i)
Next
Return reversedString
EndFunction
EndModule

6. Write a program that prompts the user for a String value and a character value. The
program
should then find the last occurrence of the provided character in the provided String and
display
the corresponding index. If the character is not found in the String, display -1.
Sample output for values "Hello, World!" and 'l':
Last occurrence of character 'l' in "Hello World" is at index 10
ModuleModule1
Sub Main()
[Link]("Enter a string: ")
Dim inputString AsString = [Link]()
[Link]("Enter a character: ")
Dim inputChar AsChar
inputChar = [Link]()
Dim lastIndex AsInteger = FindLastOccurrence(inputString, inputChar)
[Link]("Last occurrence index of '"& inputChar &" in "& inputString & lastIndex)
[Link]()
EndSub
Function FindLastOccurrence(inputString AsString, targetChar AsChar) AsInteger
Dim lastIndex AsInteger = -1
For i AsInteger = [Link] - 1 To 0 Step -1
If inputString(i) = targetChar Then
lastIndex = i
Exit For
EndIf
Next

Return lastIndex
EndFunction
EndModule
Sample output for values "Hello, World!" and 'g':
Last occurrence of character 'g' in "Hello World" is at index -1 2

7. Write a program that creates the following pattern.

******
*****
****
***
**
*
ModuleModule1
Sub Main()
For i AsInteger = 12 To 1 Step -1
For j AsInteger = 1 To i
[Link]("*")
Next j
[Link]()
Next i

[Link]()
EndSub
EndModule

8. Write a program to input a number& print all the prime numbers upto it.
ModuleModule1
Sub Main()

[Link]("Enter a number:")
Dim inputNumber AsInteger = [Link]([Link]())
[Link]("Prime numbers up to "& inputNumber &":")
For i AsInteger = 2 To inputNumber
If IsPrime(i) Then
[Link](i &" ")
EndIf
Next i
[Link]()
EndSub
Function IsPrime(number AsInteger) AsBoolean
If number <= 1 Then
ReturnFalse
EndIf
For i AsInteger = 2 [Link](number)
If number Mod i = 0 Then
ReturnFalse
EndIf
Next i
ReturnTrue
EndFunction
EndModule

9. Write a program to input a no & check whether its automorphic or not. (An
automorphic number is a number whose square "ends" in the same digits as the number
itself. For example, 52 = 25, 62 = 36, 762 = 5776, and 8906252 = 793212890625, so 5, 6, 76
and 890625 are all automorphic numbers).
ModuleModule1
Sub Main()

[Link]("Enter a number:")
Dim inputNumber AsInteger = [Link]([Link]())

If IsAutomorphic(inputNumber) Then
[Link](inputNumber &" is an automorphic number.")
Else
[Link](inputNumber &" is not an automorphic number.")
EndIf

[Link]()
EndSub

Function IsAutomorphic(number AsInteger) AsBoolean


Dim square AsLong = CLng(number) * CLng(number)
Dim strNumber AsString = [Link]()
Dim strSquare AsString = [Link]()

Dim length AsInteger = [Link]

For i AsInteger = 1 To length


If [Link]([Link] - i) <> [Link](length - i) Then
ReturnFalse
EndIf
Next

ReturnTrue
EndFunction
EndModule

10. Write a program to input a no & check whether it’s an Armstrong no or not. (An
Armstrong no is an integer such that the sum of the cubes of its digits is equal to the
number itself. For example, 371 is an Armstrong number since 33 + 73 + 13 = 371).
ModuleModule1
Sub Main()

[Link]("Enter a number:")
Dim inputNumber AsInteger = [Link]([Link]())

If IsArmstrong(inputNumber) Then
[Link](inputNumber &" is an Armstrong number.")
Else
[Link](inputNumber &" is not an Armstrong number.")
EndIf
[Link]()
EndSub
Function IsArmstrong(number AsInteger) AsBoolean
Dim originalNumber AsInteger = number
Dim numDigits AsInteger = CountDigits(number)
Dim sum AsInteger = 0
Dim tempNumber AsInteger = number
While tempNumber > 0
Dim digit AsInteger = tempNumber Mod 10
sum += CInt([Link](digit, numDigits))
tempNumber /= 10
EndWhile
Return sum = originalNumber
EndFunction
Function CountDigits(number AsInteger) AsInteger
Dim count AsInteger = 0

While number > 0


number /= 10
count += 1
EndWhile

Return count
EndFunction
EndModule

11. Write a program to check whether a given number is magic number or not.
(What is a magic number? Example: 1729
• Find the sum of digits of the given number. (1 + 7 + 2 + 9 => 19)
• Reverse of digit sum output. Reverse of 19 is 91
• Find the product of digit sum and the reverse of digit sum. (19 X 91 = 1729)
• If the product of digit sum and the reverse of digit sum is equal to the input number , then the
given number is a magic number. (19 X 91 <=> 1729)
ModuleModule1
Sub Main()

[Link]("Enter a number:")
Dim inputNumber AsInteger = [Link]()
If IsMagicNumber(inputNumber) Then
[Link](inputNumber &" is a magic number.")
Else
[Link](inputNumber &" is not a magic number.")
EndIf

[Link]()
EndSub

Function IsMagicNumber(number AsInteger) AsBoolean


While number >= 10
Dim sum AsInteger = 0
While number > 0
sum += number Mod 10
number /= 10
EndWhile
number = sum
EndWhile

Return number = 1
EndFunction
EndModule

12. Write a program to calculate generic root of the given number. (To find the generic
root of a no we first find the sum of digits of the no until we get a single digit output. That
resultant no is called the generic no. Eg: 456791: 4+5+6+7+9+1=32. 3 +2 =5. So, 5 becomes
the generic root of the given no)
ModuleModule1
Sub Main()

[Link]("Enter a number:")
Dim inputNumber AsInteger = [Link]()

Dim root AsInteger = CalculateGenericRoot(inputNumber)


[Link]("Generic root of "& inputNumber &": "& root)

[Link]()
EndSub

Function CalculateGenericRoot(number AsInteger) AsInteger


While number >= 10
Dim sum AsInteger = 0
While number > 0
sum += number Mod 10
number /= 10
EndWhile
number = sum
EndWhile

Return number
EndFunction
EndModule

[Link] that show equilateral triangle of star pattern


Sub Main()
DrawPattern(5)
[Link]()
EndSub
Sub DrawPattern(n AsInteger)
For i = 1 To n Step 1
[Link](NewString(" ", n - i) &NewString("*", i * 2 - 1))
Next
EndSub
EndModule
TASK:ASSIGNMENT NO:02(BY FUNCTION)

1. Write a program that prompts the user for an integer and displays if the provided
integer is a
prime number or not. A prime number is a number that is divisible only by 1 and itself.
First few
prime numbers are 2,3,5,7,11,13 and so on. Sample run is shown beloW
ModuleModule1
SubMain()
[Link]("Enter a number: ")
Dim num AsInteger = [Link]()
[Link](If(IsPrime(num), num &" is a prime number", num &" is not a prime
number"))
[Link]()
EndSub
FunctionIsPrime(ByVal n AsInteger) AsBoolean
If n <= 1 Then
ReturnFalse
EndIf
ForiAsInteger = 2 [Link](n)
If n Modi = 0 Then
ReturnFalse
EndIf
Next
ReturnTrue
EndFunction
EndModule

2. Write a program that prints the first 30 values in the Fibonacci series. A Fibonacci series
begins with 0 and 1. The next number is then found by adding the previous two numbers.
The first few numbers in the Fibonacci series are: 0,1,1,2,3,5,8,13 and so on.
Module FibonacciSeries
SubMain()
DimiAsInteger
Fori = 1 To 30
[Link](Fibonacci(i))
Next
[Link]()
EndSub
FunctionFibonacci(n AsInteger) AsInteger
If n <= 1 Then
Return n
Else
ReturnFibonacci(n - 1) + Fibonacci(n - 2)
EndIf
EndFunction
EndModule

3. Write a program that prompts the user for an integer value. The program should then
calculate and print the factorial of the user provided value. Factorial of a number, n,
written as n! is calculated as a product of all integers less than or equal to n. 5! = 5*4*3*2*1
= 120. 0! = 1. 1! = 1.
ModuleModule1
SubMain()
Dim num AsInteger
[Link]("Enter a number: ")
num = [Link]()
[Link]("The factorial of "& num &" is "& Factorial(num))
[Link]()
EndSub
FunctionFactorial(ByVal n AsInteger) AsInteger
If n = 0 Or n = 1 Then
Return 1
Else
Return n * Factorial(n - 1)
EndIf
EndFunction
EndModule

4. Write a program that accepts an integer (of any number of digits) from the user and
displays the sum of the digits of the provided integer. Sample output for value 235: Sum of
digits of 235 is 10.
Module Module1
Sub Main()
[Link]("Enter a number: ")
Dim num As Integer = [Link]()
[Link]("Sum of digits of " & num & " is " &SumOfDigits(num))
End Sub

Function SumOfDigits(ByVal num As Integer) As Integer


Dim sum As Integer = 0
While num <> 0
sum += num Mod 10
num \= 10
End While
Return sum
End Function
End Module

5. Write a program that accepts a String value from the user and displays the reverse of
that value.
Sample output for value "Hello, World!":
Reverse of "Hello, World!" is "!dlroW ,olleH"
ModuleModule1
SubMain()
[Link]("Enter a string: ")
Dim str AsString = [Link]()
[Link]("Reverse of the word is "&ReverseString(str))
[Link]()
EndSub
FunctionReverseString(ByVal str AsString) AsString
DimrevStrAsString
ForiAsInteger = [Link] - 1 To 0 Step -1
revStr&= str(i)
Next
ReturnrevStr
EndFunction
EndModule

For additional challenge, determine if the String and its reverse are equal and display a message
explaining the result.
Sample output for value "Hello, World!":
ModuleModule1
SubMain()

[Link]("Enter a string:")
DimuserInputAsString = [Link]()
DimreversedStringAsString = ReverseString(userInput)
[Link]("Reverse of the string: "&reversedString)
[Link](reversedString) Then
[Link]("The original string is equal to its reverse.")
Else
[Link]("The original string is not equal to its reverse.")
EndIf
[Link]()
EndSub
FunctionReverseString(inputStringAsString) AsString
DimreversedStringAsString = ""
ForiAsInteger = [Link] - 1 To 0 Step -1
reversedString&= inputString(i)
Next
ReturnreversedString
EndFunction
EndModule

6. Write a program that prompts the user for a String value and a character value. The
program
should then find the last occurrence of the provided character in the provided String and
display
the corresponding index. If the character is not found in the String, display -1.
Sample output for values "Hello, World!" and 'l':
Last occurrence of character 'l' in "Hello World" is at index 10
ModuleModule1
SubMain()
[Link]("Enter a string: ")
DiminputStringAsString = [Link]()
[Link]("Enter a character: ")
DiminputCharAsChar
inputChar = [Link]()
DimlastIndexAsInteger = FindLastOccurrence(inputString, inputChar)
[Link]("Last occurrence index of '"&inputChar&" in "&inputString&lastIndex)
[Link]()
EndSub
FunctionFindLastOccurrence(inputStringAsString, targetCharAsChar) AsInteger
DimlastIndexAsInteger = -1

ForiAsInteger = [Link] - 1 To 0 Step -1


IfinputString(i) = targetCharThen
lastIndex = i
Exit For
EndIf
Next

ReturnlastIndex
EndFunction
EndModule

Sample output for values "Hello, World!" and 'g':


Last occurrence of character 'g' in "Hello World" is at index -1 2
7. Write a program that creates the following pattern.
******
*****
****
***
**
*
ModuleModule
subMain()
DrawPattern(6)
[Link]()
EndSub

FunctionDrawPattern(ByVal n AsInteger) AsInteger


Fori = 1 To 12 Step 1
For j = 1 Toi
[Link]("*")
Next j
[Link]()
Nexti
EndFunction
EndModule

8. Write a program to number& print all the prime numbers upto it. input a
ModuleModule1
SubMain()
[Link]("Enter a number:")
DiminputNumberAsInteger = [Link]([Link]())

[Link]("Prime numbers up to "&inputNumber&":")


ForiAsInteger = 2 ToinputNumber
IfIsPrime(i) Then
[Link](i&" ")
EndIf
Nexti
[Link]()
EndSub
FunctionIsPrime(number AsInteger) AsBoolean
If number <= 1 Then
ReturnFalse
EndIf
ForiAsInteger = 2 [Link](number)
If number Modi = 0 Then
ReturnFalse
EndIf
Nexti
ReturnTrue
EndFunction
EndModule

9. Write a program to input a no & check whether its automorphic or not. (An
automorphic number is a number whose square "ends" in the same digits as the number
itself. For example, 52 = 25, 62 = 36, 762 = 5776, and 8906252 = 793212890625, so 5, 6, 76
and 890625 are all automorphic numbers).
ModuleModule1
SubMain()

[Link]("Enter a number:")
DiminputNumberAsInteger = [Link]([Link]())

IfIsAutomorphic(inputNumber) Then
[Link](inputNumber&" is an automorphic number.")
Else
[Link](inputNumber&" is not an automorphic number.")
EndIf

[Link]()
EndSub

FunctionIsAutomorphic(number AsInteger) AsBoolean


Dim square AsLong = CLng(number) * CLng(number)
DimstrNumberAsString = [Link]()
DimstrSquareAsString = [Link]()
Dim length AsInteger = [Link]
ForiAsInteger = 1 To length
[Link]([Link] - i) <>[Link](length - i) Then
ReturnFalse
EndIf
Next
ReturnTrue
EndFunction
EndModule
10. Write a program to input a no & check whether it’s an Armstrong no or not. (An
Armstrong no is an integer such that the sum of the cubes of its digits is equal to the
number itself. For example, 371 is an Armstrong number since 33 + 73 + 13 = 371).
ModuleModule1
SubMain()

[Link]("Enter a number:")
DiminputNumberAsInteger = [Link]([Link]())

IfIsArmstrong(inputNumber) Then
[Link](inputNumber&" is an Armstrong number.")
Else
[Link](inputNumber&" is not an Armstrong number.")
EndIf
[Link]()
EndSub
FunctionIsArmstrong(number AsInteger) AsBoolean
DimoriginalNumberAsInteger = number
DimnumDigitsAsInteger = CountDigits(number)
Dim sum AsInteger = 0
DimtempNumberAsInteger = number
WhiletempNumber> 0
Dim digit AsInteger = tempNumberMod 10
sum += CInt([Link](digit, numDigits))
tempNumber /= 10
EndWhile
Return sum = originalNumber
EndFunction

FunctionCountDigits(number AsInteger) AsInteger


Dim count AsInteger = 0
While number > 0
number /= 10
count += 1
EndWhile
Return count
EndFunction
EndModule

11. Write a program to check whether a given number is magic number or not.
(What is a magic number? Example: 1729
• Find the sum of digits of the given number. (1 + 7 + 2 + 9 => 19)
• Reverse of digit sum output. Reverse of 19 is 91
• Find the product of digit sum and the reverse of digit sum. (19 X 91 = 1729)
• If the product of digit sum and the reverse of digit sum is equal to the input number , then
the given number is a magic number. (19 X 91 <=> 1729)
ModuleModule1
SubMain()

[Link]("Enter a number:")
DiminputNumberAsInteger = [Link]()

IfIsMagicNumber(inputNumber) Then
[Link](inputNumber&" is a magic number.")
Else
[Link](inputNumber&" is not a magic number.")
EndIf
[Link]()
EndSub
FunctionIsMagicNumber(number AsInteger) AsBoolean
While number >= 10
Dim sum AsInteger = 0
While number > 0
sum += number Mod 10
number /= 10
EndWhile
number = sum
EndWhile

Return number = 1
EndFunction
EndModule

12. Write a program to calculate generic root of the given number. (To find the generic
root of a no we first find the sum of digits of the no until we get a single digit output. That
resultant no is called the generic no. Eg: 456791: 4+5+6+7+9+1=32. 3 +2 =5. So, 5 becomes
the generic root of the given no)
ModuleModule1
SubMain()

[Link]("Enter a number:")
DiminputNumberAsInteger = [Link]()

Dim root AsInteger = CalculateGenericRoot(inputNumber)


[Link]("Generic root of "&inputNumber&": "& root)
[Link]()
EndSub

FunctionCalculateGenericRoot(number AsInteger) AsInteger


While number >= 10
Dim sum AsInteger = 0
While number > 0
sum += number Mod 10
number /= 10
EndWhile
number = sum
EndWhile

Return number
EndFunction
EndModule

13. Program that show equilateral triangle of star pattern


.SubMain()
DrawPattern(5)
[Link]()
EndSub

FunctionDrawPattern(ByVal n AsInteger) AsInteger


Fori = 1 To nStep 1
[Link](NewString(" ", n - i) &NewString("*", i * 2 - 1))
Next
EndFunction
EndModule

EXERCISE PROGRAMING QN
[Link] a program that prompts the user to enter three test scores and then displays the
user’s test
Score average.
ANSWER
Module Module1
Sub Main()
‘ Prompt user to enter test scores
[Link](“Enter Test Score 1: “)
Dim score1 As Double = [Link]([Link]())
[Link](“Enter Test Score 2: “)
Dim score2 As Double = [Link]([Link]())
[Link](“Enter Test Score 3: “)
Dim score3 As Double = [Link]([Link]()
Dim average As Double = GetAverage(score1, score2, score3)
[Link]($”Your Test Score Average is: {average}”)
[Link]() ‘ Keep console window open
End Sub
Function GetAverage(ByVal score1 As Double, ByVal score2 As Double, ByVal score3 As
Double) As Double
‘ Calculate and return the average
Return (score1 + score2 + score3) / 3
End Function
End Module
[Link] a program that displays the following:
*****
****
***
**
*
Module Module1
Sub Main()
DisplayPattern(5)
[Link]()
End Sub
Sub DisplayPattern(ByVal rows As Integer)
For i As Integer = 5To 1 Step -1
For j As Integer = 1 To i
[Link](“* “)
Next
[Link]()
Next
End Sub
End Module

3. Write a program that converts temperatures from either Celsius to Fahrenheit or from
Fahrenheit
to Cesius. The user should enter a temperature followed by a letter tosignify which way to
convert the
temperature. The program should then display the converted temperature. For example,
ifthe user enters
100 c The program will output 212 f.
Module Module1
Sub Main()
[Link](“Enter temperature followed by ‘c’ for Celsius or ‘f’ for Fahrenheit: “)
Dim input As String = [Link]()
Dim temperatureValue As Double
[Link]([Link](0, [Link] – 1), temperatureValue)
Dim unit As Char = [Link]()([Link] – 1)
‘ Call the ConvertTemperature function and display the result
Dim convertedTemperature As Double = ConvertTemperature(temperatureValue, unit)
[Link]($”Converted Temperature: {convertedTemperature} {(If(unit = “C”c, “F”,
“C”))}”)
[Link]() ‘ Keep console window open
End Sub
Function ConvertTemperature(ByVal temperature As Double, ByVal unit As Char) As
Double
‘ Function to convert temperature based on user input
If unit = “C”c Then
‘ Convert Celsius to Fahrenheit
Return (temperature * 9 / 5) + 32
ElseIf unit = “F”c Then
‘ Convert Fahrenheit to Celsius
Return (temperature – 32) * 5 / 9
Else
‘ Invalid input
Return 0
End If
End Function
End Module
[Link] a For Next loop, write a program that sums the integers 1–100. Rewrite the
program using
A Do . . . Whie loop
Module Module1
Sub Main()
‘ Using For Next loop
Dim sumForLoop As Integer = SumUsingForLoop()
[Link]($”Sum using For Next loop: {sumForLoop}”)
‘ Using Do While loop
Dim sumDoWhileLoop As Integer = SumUsingDoWhileLoop()
[Link]($”Sum using Do While loop: {sumDoWhileLoop}”)
[Link]() ‘ Keep console window open
End Sub
Function SumUsingForLoop() As Integer
‘ Sum integers from 1 to 100 using For Next loop
Dim sum As Integer = 0
For i As Integer = 1 To 100
Sum += i
Next
Return sum
End Function
Function SumUsingDoWhileLoop() As Integer
‘ Sum integers from 1 to 100 using Do While loop
Dim sum As Integer = 0
Dim i As Integer = 1
Do While i<= 100
Sum += i
I += 1
Loop
Return sum
End Function
End Module
[Link] a Booean function that returns True if a number is even and Fase if the number is
odd. Use
The function in a program
ANSWER
Module Module1
Sub Main()
[Link](“Enter a number: “)
Dim userInput As Integer = Convert.ToInt32([Link]())
‘ Call the IsEven function and display the result
Dim isEvenResult As Boolean = IsEven(userInput)
If isEvenResult Then
[Link](“The number is even.”)
Else
[Link](“The number is odd.”)
End If
[Link]() ‘ Keep console window open
End Sub
Function IsEven(ByVal number As Integer) As Boolean
‘ Function to check if a number is even
Return number Mod 2 = 0
End Function
End ModulE
[Link] a string function that returns the reverse of any string given to it as an argument.
Use the
Function in a program.
Module Module1
Sub Main()
‘ Prompt user for a string
[Link](“Enter a string: “)
Dim userInput As String = [Link]()
‘ Call the ReverseString function and display the result
Dim reversedString As String = ReverseString(userInput)
[Link]($”Reversed String: {reversedString}”)
[Link]() ‘ Keep console window open
End Sub
Function ReverseString(ByValinputString As String) As String
‘ Function to reverse a string
Dim charArray() As Char = [Link]()
[Link](charArray)
Return New String(charArray)
End Function
End Module
[Link] a subroutine that prints the following banner when called:
**************************
* Helo, world! *
**************************
Use the subroutine in a program
Module Module1
Sub Main()
‘ Call the PrintBanner subroutine
PrintBanner()
[Link]() ‘ Keep console window open
End Sub
Sub PrintBanner()
‘ Subroutine to print the specified banner
[Link](“* * * * * * * * * * * * * * * * * * * * * * * * * *”)
[Link](“* Hello, world! *”)
[Link](“* * * * * * * * * * * * * * * * * * * * * * * * * *”)
End Sub
End Module

[Link] recursion, write a function that determines if the string given to the function is a
palindrome. A palindrome is a word that is spelled the same both forward and backward.
For
example, “bob” is a palindrome.
Module Module1
Sub Main()
‘ Prompt user for a string
[Link](“Enter a string: “)
Dim userInput As String = [Link]()
‘ Check if the entered string is a palindrome using the IsPalindrome function
If IsPalindrome(userInput) Then
[Link](“The entered string is a palindrome.”)
Else
[Link](“The entered string is not a palindrome.”)
End If
[Link]() ‘ Keep console window open
End Sub
Function IsPalindrome(ByValstr As String) As Boolean
‘ Recursive function to check if a string is a palindrome
If [Link]<= 1 Then
Return True
Else
Return (str(0) = str([Link] – 1)) And Also Is Palindrome ([Link](1, [Link] –
2))
End If
End Function
End Module

You might also like