Tin 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
1 8759
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 -
Private Sub cmdFactorial_Click()
-
Dim dblN As Double
-
Dim dblFactorial As Double
-
-
If IsNumeric(TextBox1.Text) _
-
And Len(TextBox1.Text) < 4 Then
-
dblN = CLng(TextBox1.Text)
-
If dblN > 170 Then
-
MsgBox "Please enter a numeric value between 1 and 170"
-
TextBox1.Text = ""
-
TextBox1.SetFocus
-
Exit Sub
-
End If
-
Else
-
MsgBox "Please enter a numeric value between 1 and 170"
-
TextBox1.Text = ""
-
TextBox1.SetFocus
-
Exit Sub
-
End If
-
dblFactorial = Factorial(dblN)
-
TextBox1.Text = dblFactorial
-
End Sub
-
-
Private Function Factorial(dblNum As Double) As Double
-
If dblNum - 1 = 0 Then
-
Factorial = dblNum
-
Exit Function
-
End If
-
Factorial = dblNum * Factorial(dblNum - 1)
-
End Function
-
Sign in to post your reply or Sign up for a free account.
Similar topics |
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
|
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;
|
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. ";
|
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
|
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
| |
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...
|
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(){
|
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...
|
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,...
|
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,...
|
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...
| |
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...
|
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();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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...
| |