I am trying to write code for a program in Visual Studo using Visual Basic programming...

50.1K

Verified Solution

Question

Programming

I am trying to write code for a program in Visual Studo usingVisual Basic programming language that computes the factorial of anentered number by using a For Loop. My problem is that it keepsre-setting the variable for Factorial. Below is my code if anyonecan help. I want it to multiply the given number by that number -1, then continuing to do so until it hits zero without multiplyingby zero.

Private Sub BtnCalculate_Click(sender As Object, e As EventArgs)Handles btnCalculate.Click

Dim Factorial As Integer = Integer.Parse(txtNumber.Text)

For i = Factorial To 2 Step -1
Factorial = i * (i - 1)
Next

txtFactorial.Text = Factorial.ToString()

End Sub

Answer & Explanation Solved by verified expert
3.8 Ratings (354 Votes)
Answer Your logic and code is correct but just look at main line Factorial i i 1 and process of for loop In for loop the variable is initialized only once So for first time the i and factorial will be same and second and onwards    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students