473,508 Members | 2,152 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Calculate the factorial

1 New Member
Dear Sir/Madam,

I have one assignment , which need me to

write a program to calculate the factorial of an integer,

then press the button that would display the result in a label.3

& the program should warn the user if the

iinput is not a number.

I have tried my best to write ,but It show me

something wrong,

What I write is as following, Please help me to show what is going wrong !
Thanks !

Tin



Dim N As Integer = CInt(TextBox1.Text)
Dim sum As Integer
Dim Factorial As Integer

Try

N = Integer.Parse(TextBox1.Text)

sum = N * Factorial(N - 1)

Label3.Text = CStr(sum)

Catch ex As FormatException
MessageBox.Show("Number is not an integer. Please enter an Integer ")


End Try

End Sub



Function Factorial(ByVal N As Integer) As Integer
If N = 0 Then
Return 1
Else
Return N * Factoria((N - 1) - 1)
End If
End Function
Nov 9 '06 #1
1 8731
willakawill
1,646 Top Contributor
Dear Sir/Madam,

I have one assignment , which need me to

write a program to calculate the factorial of an integer,

then press the button that would display the result in a label.3

& the program should warn the user if the

iinput is not a number.

I have tried my best to write ,but It show me

something wrong,

What I write is as following, Please help me to show what is going wrong !
Thanks !

Tin



Dim N As Integer = CInt(TextBox1.Text)
Dim sum As Integer
Dim Factorial As Integer

Try

N = Integer.Parse(TextBox1.Text)

sum = N * Factorial(N - 1)

Label3.Text = CStr(sum)

Catch ex As FormatException
MessageBox.Show("Number is not an integer. Please enter an Integer ")


End Try

End Sub



Function Factorial(ByVal N As Integer) As Integer
If N = 0 Then
Return 1
Else
Return N * Factoria((N - 1) - 1)
End If
End Function
Hi, a couple of things might help.
The Try...Catch block is C++ programming and is not part of VB6
You need to make sure that the input number is 171 or less or you will get an overflow error as the factorial of 172 is too large to store in a Double.
You have declared an integer with the same name as the function.

The following works

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdFactorial_Click()
  2.  Dim dblN As Double
  3.  Dim dblFactorial As Double
  4.  
  5. If IsNumeric(TextBox1.Text) _
  6. And Len(TextBox1.Text) < 4 Then
  7.    dblN = CLng(TextBox1.Text)
  8.    If dblN > 170 Then
  9.       MsgBox "Please enter a numeric value between 1 and 170"
  10.       TextBox1.Text = ""
  11.       TextBox1.SetFocus
  12.       Exit Sub
  13.    End If
  14. Else
  15.    MsgBox "Please enter a numeric value between 1 and 170"
  16.    TextBox1.Text = ""
  17.    TextBox1.SetFocus
  18.    Exit Sub
  19. End If
  20. dblFactorial = Factorial(dblN)
  21. TextBox1.Text = dblFactorial
  22. End Sub
  23.  
  24. Private Function Factorial(dblNum As Double) As Double
  25.    If dblNum - 1 = 0 Then
  26.       Factorial = dblNum
  27.       Exit Function
  28.    End If
  29.    Factorial = dblNum * Factorial(dblNum - 1)
  30. End Function
  31.  
Nov 9 '06 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

33
9901
by: patrick_woflian | last post by:
hey guys, im just writing a basic calculation at the moment, before building on it for an A-Level piece of work. i can add/divide etc... two numbers together yet i am having a major problem with...
35
3911
by: aNt17017 | last post by:
This is my code: long fact(int n) { if (n == 0) return(1); if(n > 100) { printf("\t\tERROR: %d is too large for factorial.\n", n); return 1;
59
55670
by: Umesh | last post by:
i wrote the following program to calculate factorial: #include<stdio.h> #include<iostream.h> void main() { int i,n; long int p=1; // or long double p=1; for exponential result which I don't...
3
8004
by: Sugandh Jain | last post by:
Hi. How to write a function that will return me the factorial (say in a string) for the any positive integer it takes? When we find a factorial of even say 2000 or a higher number, it will be...
0
25158
by: Killer42 | last post by:
Here's a very simple function for VB (written in VB6) to calculate the factorial (!) of a number. Note that it is limited by the magnitude of the value which can be stored in the Long data type. (In...
0
12256
kadghar
by: kadghar | last post by:
Hi, I saw that Killer posted a simple Factorial Function that allows you to calculate up to 13!, well, you can use this for bigger numbers by changing the variable type. Why is this? You can...
2
4879
by: becky808 | last post by:
Hi, I'm trying to write a program to calculate n factorial but it won't compile. Can anyone tell me what I'm doing wrong? #include <iostream> #include <cmath> using namespace std; int...
1
7046
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5629
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5053
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4707
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3195
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3182
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1558
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
766
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
418
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.