Write Algorithm and Flowchart to implement a program to accept a number in a Textbox and display the result of Factorial of that number in another TextBox.
Algorithm
Start
Declare the variable number
Declare the variable factorial =1
If number < 0 Then
Display the message: "Factorial of negative number is not possible"
ElseIf number = 0 Or number = 1 Then
factorial=1
Display the factorial
Else
Calculate the factorial using for loop
For i = 1 To number
factorial = factorial * i
Display the factorial
End If
Stop
Visual Basic Code:
Public Class frmFactorialNumber
''' <summary>
''' Calculate button
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
'Declare variable number
Dim number As Integer
'Declare variable factorial
Dim factorial As Integer = 1
'check if number is correct
If Integer.TryParse(txtNumber.Text, number) = False Or number < 0 Then
'Display the message: "Factorial of negative number is not possible"
MessageBox.Show("Factorial of negative number is not possible")
ElseIf number = 0 Or number = 1 Then
'Display the factorial
txtFactorial.Text = factorial.ToString()
Else
'Calculate the factorial using for loop
For i = 1 To number
factorial = factorial * i
Next
'Display the factorial
txtFactorial.Text = factorial.ToString()
End If
End Sub
End Class
Flowchart
Example:
Comments
Leave a comment