Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hello everyone, I was wondering you guys can help me with my visual basic assign

ID: 3913171 • Letter: H

Question

Hello everyone, I was wondering you guys can help me with my visual basic assignment.

Create a Windows Forms application that has the following:

Define a function named IsPositive, which has one parameter as Integer. If the number in the parameter is positive (>= 0) the function returns True, otherwise it returns False.

A button showing the text Factorial. When clicked this button uses a function named Factorial passing it a number typed in a textbox named NumEntrytxt. Then check the return value of the Factorial function, using IsPositive. If it is positive then show the factorial value in textbox named Resulttxt. Otherwise show the message "Data entered has no defined factorial".

A button showing the text Prime ?.When clicked this button uses a function named IsPrime passing it a number typed in a textbox named NumEntrytxt, then it shows the result in a textbox named Resulttxt. If the function returns True, the result should be "X is Prime", otherwise show "X is not Prime", where X is the actual number passed to the function.

Write the function definition of Factorial, which has one integer parameter and uses the IsPositive function to check if the number passed is positive, then it returns the factorial of that number (code to calculate factorial is in the book in an earlier chapter). If the number is not positive, Factorial returns -1 (meaning there is an error).

Write the function definition of IsPrime, which has one integer parameter and uses the IsPositive function to check if the number passed is positive, then it returns True if the number pass is indeed a prime number, otherwise return False. Code to check if a number is a prime number, is in the book in an earlier chapter.

Important Note: From this assignment on, there will be a 0.25 deduction for every Option Strict violation.

PS: I am aware that there is already a post similar to this one. I'm looking for something unique incase someone already used it for thier assignment which I have encountered in the past. Code only is fine, I can do the design myself. Thank you for your efforts, I appreciate your time.

Explanation / Answer

Public Class Form1
'Function check a number is postive or not and return the result as true or false
Private Function IsPositive(ByVal n As Integer) As Boolean
If n >= 0 Then
Return True
Else
Return False
End If
End Function
' Function compute factorial of a number
Private Function Factorial(ByVal n As Integer) As String
Dim result As String
Dim fact As Long = 1
If Not IsPositive(n) Then
result = "Data entered has no defined factorial"
Else
For i = 2 To n
fact = fact * i
Next
result = CStr(fact)
End If
Return result
End Function
' function check a number is prime or not and return true or false as result
Private Function IsPrime(ByVal X As Integer) As Boolean
Dim i As Integer, cnt As Integer = 0
If X = 0 Then
Return False
ElseIf Not IsPositive(X) Then
Return False
Else
For i = 2 To CInt(X / 2)
If X Mod i = 0 Then
cnt = cnt + 1
End If
Next

If cnt = 0 Then
Return True
Else
Return False
End If
End If
  
End Function

'Factorial button click event
Private Sub btnFactorial_Click(sender As Object, e As EventArgs) Handles btnFactorial.Click
Dim num As Integer
num = CInt(NumEntrytxt.Text)
Resulttxt.Text = Factorial(num)
End Sub

'Prime button click event
Private Sub btnPrime_Click(sender As Object, e As EventArgs) Handles btnPrime.Click
Dim num As Integer
num = CInt(NumEntrytxt.Text)
If IsPrime(num) Then
Resulttxt.Text = num & " is Prime"
Else
Resulttxt.Text = num & " is not Prime"
End If
End Sub
End Class

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote