We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,555 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Factorial of N

I really dont know this Program is rite or wrong. but it wont give a answer. if anyone can help me in this

Public Class Form2
Dim num As Integer

Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim fact As Integer

num = Val(InputBox("Enter the Factorial Number"))


Do
fact = fact * num
num = num - 1

Loop Until num <= 0


MessageBox.Show(fact)

End Sub
End Class

2
Contributors
1
Reply
2 Hours
Discussion Span
5 Years Ago
Last Updated
2
Views
lich
Junior Poster in Training
79 posts since May 2008
Reputation Points: -1
Solved Threads: 4
Skill Endorsements: 0

use this following function :

Public Function Factorial(ByVal Number As Integer) As String
        Try
            If Number = 0 Then
                Return 1
            Else
                Return Number * Factorial(Number - 1)
            End If
        Catch ex As Exception
            Return ex.Message
        End Try
    End Function

I called this function in button click event:

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
       Dim num As Integer
        num = Val(InputBox("Enter the Factorial Number"))
        MsgBox(Factorial(num))
End Sub
Jx_Man
Senior Poster
3,553 posts since Nov 2007
Reputation Points: 1,500
Solved Threads: 531
Skill Endorsements: 65

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page generated in 0.0592 seconds using 2.71MB