473,666 Members | 2,048 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.T ext)
Dim sum As Integer
Dim Factorial As Integer

Try

N = Integer.Parse(T extBox1.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 8759
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.T ext)
Dim sum As Integer
Dim Factorial As Integer

Try

N = Integer.Parse(T extBox1.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
9923
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 the following calculation: z = x! / (x- y)! The following code is my attempt and i was hoping for a point in the right direction as too where i am going wrong.. cheers
35
3960
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
55721
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 want. cout<<"Enter No. ";
3
8011
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 very big to be accomodated in int or long datatype. Regards, Sugandh
0
25366
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 fact, I believe it overflows at 13!) Public Function Factorial(ByVal bNum As Byte) As Long Dim I As Long If bNum <= 0 Then Exit Function Factorial = 1 For I = 1 To bNum Factorial = Factorial * I Next End Function
0
12297
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 use factorial on integers (that should be the input), the output could be a string or a double, anyway you will alwas have the factorial. The only restriction is in the input. Yes, i mean this function works fine for values up to 170! or something...
2
4886
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 main(){
0
8445
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8356
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8551
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8640
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6198
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5664
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4198
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4369
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1776
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.