0% found this document useful (0 votes)
2 views16 pages

Event Driven Programming

The document provides a series of Visual Basic (VB.NET) programming examples, including creating variables, accepting user input, performing arithmetic operations, checking even/odd numbers, finding the largest element in an array, calculating the area of a rectangle, and finding the third angle of a triangle. Each example includes complete code snippets and explanations of how the code works. The document also offers options for GUI versions and additional functionalities.

Uploaded by

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

Event Driven Programming

The document provides a series of Visual Basic (VB.NET) programming examples, including creating variables, accepting user input, performing arithmetic operations, checking even/odd numbers, finding the largest element in an array, calculating the area of a rectangle, and finding the third angle of a triangle. Each example includes complete code snippets and explanations of how the code works. The document also offers options for GUI versions and additional functionalities.

Uploaded by

tesfayeurgesa916
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

1.

Create variables with names ‘Name’, ‘age’, and ‘id’ in visual basic programming language

Here is how you can create variables named Name, age, and id in Visual Basic ([Link]) and accept input
from the user:

🧾 Example in Visual Basic (Console Application)

Module Module1

Sub Main()

' Declare variables

Dim Name As String

Dim age As Integer

Dim id As String

' Get user input

[Link]("Enter your name: ")

Name = [Link]()

[Link]("Enter your age: ")

age = Convert.ToInt32([Link]())

[Link]("Enter your ID: ")

id = [Link]()

' Display output

[Link]("Hello " & Name & ", you are " & age & " years old")

' Pause the console

[Link]()

End Sub

End Module

📌 Notes:

[Link]() is used to take user input.

Convert.ToInt32() converts age input from string to integer.

[Link]() displays the formatted output.

Let me know if you want a GUI version (using Windows Forms).


Accept values for Name, age, and id from a user in visual basic programming language ([Link])

To accept values for Name, age, and id from a user in [Link] (Visual Basic .NET), you typically do this in
a Console Application using [Link]().
Here's a complete example:
✅ [Link] Code to Accept User Input for Name, age, and id:
Module Module1
Sub Main()
' Declare variables
Dim Name As String
Dim age As Integer
Dim id As String
' Get user input
[Link]("Enter your Name: ")
Name = [Link]()
[Link]("Enter your Age: ")
age = Convert.ToInt32([Link]())
[Link]("Enter your ID: ")
id = [Link]()
' Output the results
[Link]("Hello " & Name & ", you are " & age & " years old.")
' Keep the console window open
[Link]()
End Sub
End Module
💡 Explanation:
Dim declares a variable.
[Link]() gets input from the user as a string.
Convert.ToInt32() converts the age input to an integer.
& is used to concatenate strings in [Link].
You can paste this code into Visual Studio under a Console App (.NET Framework) project to test it.
Would you like the same code in a GUI (Windows Forms) version?
Display the values on the console as “Hello ‘Name’ you are ‘age’ years old” � in visual basic
programming language ([Link])

Here’s how you can display the values on the console in [Link] exactly as:
> “Hello ‘Name’ you are ‘age’ years old”--
✅ Full [Link] Console Example:
Module Module1
Sub Main()
' Declare variables
Dim Name As String
Dim age As Integer
Dim id As String
' Get user input
[Link]("Enter your Name: ")
Name = [Link]()
[Link]("Enter your Age: ")
age = Convert.ToInt32([Link]())
[Link]("Enter your ID: ")
id = [Link]()
' Display the formatted output
[Link]("Hello '" & Name & "' you are '" & age & "' years old")
' Pause the console
[Link]()
End Sub
End Module
🧾 Output Example:
If the user enters:
Name: Alice
Age: 25
ID: A123
The output will be:
Hello 'Alice' you are '25' years old
Let me know if you want to include the ID in the message or create a GUI version.
2. Write a VB program that accepts two numbers from a user and prints their sum. �

Here's a simple [Link] (Visual Basic) program that accepts two numbers from the user and prints
their sum.

✅ Full [Link] Console Application: Add Two Numbers

Module Module1

Sub Main()

' Declare variables

Dim num1 As Double

Dim num2 As Double

Dim sum As Double

' Get input from user

[Link]("Enter the first number: ")

num1 = [Link]([Link]())

[Link]("Enter the second number: ")

num2 = [Link]([Link]())

' Calculate the sum

sum = num1 + num2

' Display the result

[Link]("The sum of " & num1 & " and " & num2 & " is " & sum)

' Keep the console window open


[Link]()

End Sub

End Module

Sample Output:

Enter the first number: 15

Enter the second number: 25

The sum of 15 and 25 is 40

3. Write a VB program to check if a number accepted from a user is EVEN or ODD 5

Here's a simple [Link] (Visual Basic) console program that accepts a number from the user and
checks whether it is EVEN or ODD.

✅ [Link] Code: Check If Number Is Even or Odd

Module Module1

Sub Main()

' Declare variable

Dim number As Integer

' Get input from the user

[Link]("Enter a number: ")

number = Convert.ToInt32([Link]())

' Check if the number is even or odd

If number Mod 2 = 0 Then

[Link]("The number " & number & " is EVEN.")

Else
[Link]("The number " & number & " is ODD.")

End If

' Keep the console window open

[Link]()

End Sub

End Module

🧾 How it works:

Mod 2 checks the remainder when dividing by 2.

If remainder is 0, the number is even; otherwise, it's odd.

🧪 Sample Output:

Enter a number: 7

The number 7 is ODD.

Let me know if you'd like this logic used in a GUI (Windows Forms) version!

4. Write a VB program to find the largest integer array element among 10 elements.6

Here’s a [Link] (Visual Basic) console program that accepts 10 integers from the user, stores
them in an array, and then finds the largest element.

✅ [Link] Code: Find the Largest Element in an Integer Array

Module Module1

Sub Main()

' Declare an array with 10 elements

Dim numbers(9) As Integer

Dim i As Integer

Dim max As Integer


' Get 10 integers from the user

[Link]("Enter 10 integer values:")

For i = 0 To 9

[Link]("Enter number " & (i + 1) & ": ")

numbers(i) = Convert.ToInt32([Link]())

Next

' Assume the first element is the largest initially

max = numbers(0)

' Loop to find the largest element

For i = 1 To 9

If numbers(i) > max Then

max = numbers(i)

End If

Next

' Display the largest number

[Link]("The largest number is: " & max)

' Keep console open

[Link]()

End Sub

End Module

🧾 Sample Output:

Enter 10 integer values:

Enter number 1: 12
Enter number 2: 45

Enter number 3: 23

Enter number 10: 87

The largest number is: 87

Let me know if you want to sort the array, find the smallest number, or use a GUI instead!

5. Write a VB program that accepts width and height, and calculates the area of the rectangle

8Here is a simple [Link] (Visual Basic) console application that accepts width and height from
the user and calculates the area of a rectangle.

✅ [Link] Code: Calculate Area of a Rectangle

Module Module1

Sub Main()

' Declare variables

Dim width As Double

Dim height As Double

Dim area As Double

' Get width and height from user

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

width = [Link]([Link]())

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

height = [Link]([Link]())

' Calculate the area

area = width * height

' Display the result

[Link]("The area of the rectangle is: " & area)


' Keep the console window open

[Link]()

End Sub

End Module

🧾 Sample Output:

Enter the width of the rectangle: 5

Enter the height of the rectangle: 10

The area of the rectangle is: 50

Let me know if you’d like to add perimeter calculation or see a GUI version!

6. Write a VB program that accepts the two angles of a triangle from a user and finds the
third angle. 9

Here’s a [Link] (Visual Basic) console program that accepts two angles of a triangle from the
user and calculates the third angle.

✅ [Link] Code: Find the Third Angle of a Triangle

Module Module1

Sub Main()

' Declare variables

Dim angle1 As Double

Dim angle2 As Double

Dim angle3 As Double

' Get the first two angles from the user

[Link]("Enter the first angle of the triangle: ")


angle1 = [Link]([Link]())

[Link]("Enter the second angle of the triangle: ")

angle2 = [Link]([Link]())

' Calculate the third angle

angle3 = 180 - (angle1 + angle2)

' Display the third angle

[Link]("The third angle of the triangle is: " & angle3)

' Keep the console window open

[Link]()

End Sub

End Module

🧾 Sample Output:

Enter the first angle of the triangle: 60

Enter the second angle of the triangle: 80

The third angle of the triangle is: 40

🔺 Note:

The sum of the internal angles in any triangle is always 180 degrees.

This program assumes the input angles are valid (i.e., their sum is less than 180).

Let me know if you want to include input validation!


7. Create four methods with function names ‘add’, ’subtract’, ’multiply’, and ’divide’

Each methods have two parameters to accept values, calculate and display results on the
console

Prompt the user to enter two numbers

Prompt the user to choose the operation they want

Call the designated method based on the user’s choice11

Here is a Visual Basic ([Link]) console application that does what you asked:

---

✅ Program Requirements Covered:

Four methods: add, subtract, multiply, divide

Each method takes two parameters and displays the result

Prompt the user to enter two numbers

Prompt the user to choose an operation

Call the appropriate method based on user input


---

💻 Complete [Link] Code:

Module Module1

' Method to add two numbers

Sub add(a As Double, b As Double)

[Link]("Result: " & (a + b))

End Sub

' Method to subtract two numbers

Sub subtract(a As Double, b As Double)

[Link]("Result: " & (a - b))

End Sub

' Method to multiply two numbers

Sub multiply(a As Double, b As Double)

[Link]("Result: " & (a * b))

End Sub

' Method to divide two numbers


Sub divide(a As Double, b As Double)

If b <> 0 Then

[Link]("Result: " & (a / b))

Else

[Link]("Error: Cannot divide by zero.")

End If

End Sub

Sub Main()

' Prompt user for two numbers

[Link]("Enter the first number: ")

Dim num1 As Double = [Link]([Link]())

[Link]("Enter the second number: ")

Dim num2 As Double = [Link]([Link]())

' Prompt user to choose an operation

[Link]("Choose operation: +, -, *, /")

[Link]("Enter your choice: ")

Dim choice As String = [Link]()

' Call the appropriate method

Select Case choice

Case "+"
add(num1, num2)

Case "-"

subtract(num1, num2)

Case "*"

multiply(num1, num2)

Case "/"

divide(num1, num2)

Case Else

[Link]("Invalid choice.")

End Select

' Pause the console

[Link]()

End Sub

End Module

🧪 Example Run:

Enter the first number: 10

Enter the second number: 5

Choose operation: +, -, *, /

Enter your choice: *

Result: 50

Let me know if you'd like a version with a loop or GUI version (Windows Forms).
8. Write a VB program to display the following pattern on the console using nested loop

statements

22

333

4444

5 5 5 5 515

Here's a Visual Basic ([Link]) program that uses nested loops to display the following
pattern:

22

333

4444

55555

✅ [Link] Console Program:

Module Module1

Sub Main()

' Outer loop for rows (1 to 5)

For i As Integer = 1 To 5

' Inner loop for printing numbers in each row

For j As Integer = 1 To i
[Link](i & " ")

Next

' Move to the next line after each row

[Link]()

Next

' Pause the console

[Link]()

End Sub

End Module

🧠 How it works:

The outer loop (For i = 1 To 5) controls the number of rows.

The inner loop (For j = 1 To i) controls how many times the number is printed in each row.

[Link](i & " ") prints the number with a space.

[Link]() moves to the next line after each row.

Let me know if you want to modify the pattern (e.g., triangle, reverse order, etc.).

You might also like