I am planning a program to calculate factorials [closed]











up vote
-3
down vote

favorite
1












I am using a program to calculate factorials on console in VB.NET. The codes are:



Imports System
Module Program
Sub Main()
End Sub
Function Factorial(ByVal n As Integer) As Integer
Console.WriteLine("Which number would you like to be entered as a factorial number?")
n = Console.ReadLine()
If n = 0 Then
Return 1
Else
Return n * Factorial(n - 1)
End If
Console.WriteLine(Factorial)
Console.ReadLine()
End Function
End Module









share|improve this question















closed as unclear what you're asking by Blackwood, Disaffected 1070452, IvanH, gnat, Tetsuya Yamamoto Nov 12 at 2:12


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.











  • 1




    What is your question?
    – Mary
    Nov 10 at 18:02










  • The program couldn't display the numbers and when I don't use the Sub Main() function, I can't link to the function.
    – Eric
    Nov 10 at 19:51










  • The subroutine "Main" is where the program starts doing things. A function is something that you use from another part of the program. It looks like you have accidentally put the parts which belong in "Main" into the Factorial function code. (P.S. Never use recursion to calculate a factorial, always use the simple way of Dim factorial As Integer = 1 For i = 2 to n factorial = factorial * i Next Return factorial. You will know when the "never" does not apply.)
    – Andrew Morton
    Nov 10 at 21:46

















up vote
-3
down vote

favorite
1












I am using a program to calculate factorials on console in VB.NET. The codes are:



Imports System
Module Program
Sub Main()
End Sub
Function Factorial(ByVal n As Integer) As Integer
Console.WriteLine("Which number would you like to be entered as a factorial number?")
n = Console.ReadLine()
If n = 0 Then
Return 1
Else
Return n * Factorial(n - 1)
End If
Console.WriteLine(Factorial)
Console.ReadLine()
End Function
End Module









share|improve this question















closed as unclear what you're asking by Blackwood, Disaffected 1070452, IvanH, gnat, Tetsuya Yamamoto Nov 12 at 2:12


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.











  • 1




    What is your question?
    – Mary
    Nov 10 at 18:02










  • The program couldn't display the numbers and when I don't use the Sub Main() function, I can't link to the function.
    – Eric
    Nov 10 at 19:51










  • The subroutine "Main" is where the program starts doing things. A function is something that you use from another part of the program. It looks like you have accidentally put the parts which belong in "Main" into the Factorial function code. (P.S. Never use recursion to calculate a factorial, always use the simple way of Dim factorial As Integer = 1 For i = 2 to n factorial = factorial * i Next Return factorial. You will know when the "never" does not apply.)
    – Andrew Morton
    Nov 10 at 21:46















up vote
-3
down vote

favorite
1









up vote
-3
down vote

favorite
1






1





I am using a program to calculate factorials on console in VB.NET. The codes are:



Imports System
Module Program
Sub Main()
End Sub
Function Factorial(ByVal n As Integer) As Integer
Console.WriteLine("Which number would you like to be entered as a factorial number?")
n = Console.ReadLine()
If n = 0 Then
Return 1
Else
Return n * Factorial(n - 1)
End If
Console.WriteLine(Factorial)
Console.ReadLine()
End Function
End Module









share|improve this question















I am using a program to calculate factorials on console in VB.NET. The codes are:



Imports System
Module Program
Sub Main()
End Sub
Function Factorial(ByVal n As Integer) As Integer
Console.WriteLine("Which number would you like to be entered as a factorial number?")
n = Console.ReadLine()
If n = 0 Then
Return 1
Else
Return n * Factorial(n - 1)
End If
Console.WriteLine(Factorial)
Console.ReadLine()
End Function
End Module






vb.net console






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 21:36









Andrew Morton

15k53049




15k53049










asked Nov 10 at 17:57









Eric

14




14




closed as unclear what you're asking by Blackwood, Disaffected 1070452, IvanH, gnat, Tetsuya Yamamoto Nov 12 at 2:12


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.






closed as unclear what you're asking by Blackwood, Disaffected 1070452, IvanH, gnat, Tetsuya Yamamoto Nov 12 at 2:12


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.










  • 1




    What is your question?
    – Mary
    Nov 10 at 18:02










  • The program couldn't display the numbers and when I don't use the Sub Main() function, I can't link to the function.
    – Eric
    Nov 10 at 19:51










  • The subroutine "Main" is where the program starts doing things. A function is something that you use from another part of the program. It looks like you have accidentally put the parts which belong in "Main" into the Factorial function code. (P.S. Never use recursion to calculate a factorial, always use the simple way of Dim factorial As Integer = 1 For i = 2 to n factorial = factorial * i Next Return factorial. You will know when the "never" does not apply.)
    – Andrew Morton
    Nov 10 at 21:46
















  • 1




    What is your question?
    – Mary
    Nov 10 at 18:02










  • The program couldn't display the numbers and when I don't use the Sub Main() function, I can't link to the function.
    – Eric
    Nov 10 at 19:51










  • The subroutine "Main" is where the program starts doing things. A function is something that you use from another part of the program. It looks like you have accidentally put the parts which belong in "Main" into the Factorial function code. (P.S. Never use recursion to calculate a factorial, always use the simple way of Dim factorial As Integer = 1 For i = 2 to n factorial = factorial * i Next Return factorial. You will know when the "never" does not apply.)
    – Andrew Morton
    Nov 10 at 21:46










1




1




What is your question?
– Mary
Nov 10 at 18:02




What is your question?
– Mary
Nov 10 at 18:02












The program couldn't display the numbers and when I don't use the Sub Main() function, I can't link to the function.
– Eric
Nov 10 at 19:51




The program couldn't display the numbers and when I don't use the Sub Main() function, I can't link to the function.
– Eric
Nov 10 at 19:51












The subroutine "Main" is where the program starts doing things. A function is something that you use from another part of the program. It looks like you have accidentally put the parts which belong in "Main" into the Factorial function code. (P.S. Never use recursion to calculate a factorial, always use the simple way of Dim factorial As Integer = 1 For i = 2 to n factorial = factorial * i Next Return factorial. You will know when the "never" does not apply.)
– Andrew Morton
Nov 10 at 21:46






The subroutine "Main" is where the program starts doing things. A function is something that you use from another part of the program. It looks like you have accidentally put the parts which belong in "Main" into the Factorial function code. (P.S. Never use recursion to calculate a factorial, always use the simple way of Dim factorial As Integer = 1 For i = 2 to n factorial = factorial * i Next Return factorial. You will know when the "never" does not apply.)
– Andrew Morton
Nov 10 at 21:46














1 Answer
1






active

oldest

votes

















up vote
0
down vote













Borrowed @AndrewMorton's Function, just changed factorial to Long.
Your function should only do one thing, calculate the Factorial. It should not ask for input, process input or display results, just do the math and return the results to the caller. This is the Single Responsibility theory of methods which is generally accepted. If your assigned requires that you use recursion then just substiture your code in the function without any of the lines with Console. The data type has been changed to long but you will need to limit your input or you will get Arithmetic overflow.



Sub Main()
Do
Console.WriteLine("You may type Exit to end the program.")
Console.WriteLine("Which number would you like to be entered as a factorial number?")
Dim n As Long
Dim RetVal As Long
Dim input As String = Console.ReadLine
If input.ToUpper = "EXIT" Then
End
End If
Long.TryParse(input, n)
If n > 20 Then
Console.WriteLine("Number must be less than 21")
Continue Do
End If
RetVal = GetFactorial(n)
Console.WriteLine(RetVal)
Loop
End Sub

Private Function GetFactorial(ByVal n As Long) As Long
Dim factorial As Long = 1
For i = 2 To n
factorial = factorial * i
Next
Return factorial
End Function





share|improve this answer




























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    Borrowed @AndrewMorton's Function, just changed factorial to Long.
    Your function should only do one thing, calculate the Factorial. It should not ask for input, process input or display results, just do the math and return the results to the caller. This is the Single Responsibility theory of methods which is generally accepted. If your assigned requires that you use recursion then just substiture your code in the function without any of the lines with Console. The data type has been changed to long but you will need to limit your input or you will get Arithmetic overflow.



    Sub Main()
    Do
    Console.WriteLine("You may type Exit to end the program.")
    Console.WriteLine("Which number would you like to be entered as a factorial number?")
    Dim n As Long
    Dim RetVal As Long
    Dim input As String = Console.ReadLine
    If input.ToUpper = "EXIT" Then
    End
    End If
    Long.TryParse(input, n)
    If n > 20 Then
    Console.WriteLine("Number must be less than 21")
    Continue Do
    End If
    RetVal = GetFactorial(n)
    Console.WriteLine(RetVal)
    Loop
    End Sub

    Private Function GetFactorial(ByVal n As Long) As Long
    Dim factorial As Long = 1
    For i = 2 To n
    factorial = factorial * i
    Next
    Return factorial
    End Function





    share|improve this answer

























      up vote
      0
      down vote













      Borrowed @AndrewMorton's Function, just changed factorial to Long.
      Your function should only do one thing, calculate the Factorial. It should not ask for input, process input or display results, just do the math and return the results to the caller. This is the Single Responsibility theory of methods which is generally accepted. If your assigned requires that you use recursion then just substiture your code in the function without any of the lines with Console. The data type has been changed to long but you will need to limit your input or you will get Arithmetic overflow.



      Sub Main()
      Do
      Console.WriteLine("You may type Exit to end the program.")
      Console.WriteLine("Which number would you like to be entered as a factorial number?")
      Dim n As Long
      Dim RetVal As Long
      Dim input As String = Console.ReadLine
      If input.ToUpper = "EXIT" Then
      End
      End If
      Long.TryParse(input, n)
      If n > 20 Then
      Console.WriteLine("Number must be less than 21")
      Continue Do
      End If
      RetVal = GetFactorial(n)
      Console.WriteLine(RetVal)
      Loop
      End Sub

      Private Function GetFactorial(ByVal n As Long) As Long
      Dim factorial As Long = 1
      For i = 2 To n
      factorial = factorial * i
      Next
      Return factorial
      End Function





      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        Borrowed @AndrewMorton's Function, just changed factorial to Long.
        Your function should only do one thing, calculate the Factorial. It should not ask for input, process input or display results, just do the math and return the results to the caller. This is the Single Responsibility theory of methods which is generally accepted. If your assigned requires that you use recursion then just substiture your code in the function without any of the lines with Console. The data type has been changed to long but you will need to limit your input or you will get Arithmetic overflow.



        Sub Main()
        Do
        Console.WriteLine("You may type Exit to end the program.")
        Console.WriteLine("Which number would you like to be entered as a factorial number?")
        Dim n As Long
        Dim RetVal As Long
        Dim input As String = Console.ReadLine
        If input.ToUpper = "EXIT" Then
        End
        End If
        Long.TryParse(input, n)
        If n > 20 Then
        Console.WriteLine("Number must be less than 21")
        Continue Do
        End If
        RetVal = GetFactorial(n)
        Console.WriteLine(RetVal)
        Loop
        End Sub

        Private Function GetFactorial(ByVal n As Long) As Long
        Dim factorial As Long = 1
        For i = 2 To n
        factorial = factorial * i
        Next
        Return factorial
        End Function





        share|improve this answer












        Borrowed @AndrewMorton's Function, just changed factorial to Long.
        Your function should only do one thing, calculate the Factorial. It should not ask for input, process input or display results, just do the math and return the results to the caller. This is the Single Responsibility theory of methods which is generally accepted. If your assigned requires that you use recursion then just substiture your code in the function without any of the lines with Console. The data type has been changed to long but you will need to limit your input or you will get Arithmetic overflow.



        Sub Main()
        Do
        Console.WriteLine("You may type Exit to end the program.")
        Console.WriteLine("Which number would you like to be entered as a factorial number?")
        Dim n As Long
        Dim RetVal As Long
        Dim input As String = Console.ReadLine
        If input.ToUpper = "EXIT" Then
        End
        End If
        Long.TryParse(input, n)
        If n > 20 Then
        Console.WriteLine("Number must be less than 21")
        Continue Do
        End If
        RetVal = GetFactorial(n)
        Console.WriteLine(RetVal)
        Loop
        End Sub

        Private Function GetFactorial(ByVal n As Long) As Long
        Dim factorial As Long = 1
        For i = 2 To n
        factorial = factorial * i
        Next
        Return factorial
        End Function






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 10 at 22:53









        Mary

        2,5602618




        2,5602618















            Popular posts from this blog

            Full-time equivalent

            Bicuculline

            さくらももこ