Tracking Forums, Newsgroups, Maling Lists
Home Scripts Tutorials Tracker Forums
 
  HOME    TRACKER    Visual Basic




How To Factorial A Number?


How do I use visual basic to work out say 5! (5 factorial, 1x2x3x4x5)
Is there a keyword or do I have to set up some loop.
I need to be able to factorial a variable number the user inputs




View Complete Forum Thread with Replies

See Related Forum Messages: Follow the Links Below to View Complete Thread
Factorial
I have a list of 7 items, I would like to see all the different ways the list can be arranged.

One idea is as follows, but something elegant would be far more useful.

If wanting to see all the possible arrangements of the letters in the word cat:
for x = 1 to the length of the word cat
position(a) = x
for y = 1 to lenght of the word cat
if y <> x then
position(b) = y
end if
if position(b) is filled then
for z = 1 to lenght of the word cat
if z <> x then
if z <> y then
position(c) = z

etc. etc., of course, for a seven item list, the nested IF's and what nots become outrageous.

Can anyone offer any suggestions?

Factorial
Can somebody clarify how to program extraction of factorial values in VB.

When I write

X = n!
or X = (n -1)!

it appears that the program doesn't recognise " ! " as a mathematical operation, but only as an exclamation mark (as the expression becomes red with the compile error message "expected end of statement" only in the latter case).

Factorial
How would one do factorials:

5! = 120

using for loops?

Factorial
Can anyone tell me the code in vb for factorial using sub and class plz.

Factorial
A user will enter the number into a Text Box (txtFactorial) and the application will display the factorial of that number in a Label (lblFactorial).

What code is required in the cmdFactorial Command Button to accomplish this?

Factorial
please, can anybody help me about Factorial,I'm learning about vb6 and need know how to. I know that 3!=3*2*1.Thanks and sorry for my English

Factorial
Does any one know the VB representation of a "factorial." Either n! or x! on a decent calculator. example. 5! = 120
that is: 5 x 4 x 3 x 2 x 1 normally used with probability calculations etc.

How To Do Factorial In Vb??Need Help
The question is find factorial of a number.Receive input using inputbox and display using messagebox

How do i do it?If it is in C++ i know how it works but in Vb im kinda blur of how to use the module
this is what i do in module already

Public Function Factorial(Number As Integer) As Long
If Number < 1 Then
Factorial = 1
Else
Factorial = Number * Factorial(Number - 1)
End If
End Function

but my problem is how im going to use it in my main coding area
since im using an input box so bit blur and need help

Factorial Function
I need to add this formula in Visual Basic
Any ideas how i could do that?


the formula is in one big curly bracket and it has 2 lines

before the bracket it says x!
after the bracket on the first line 1 if x = 0
second line x * (x-1)! if x> o

Code For Factorial
I searched some of the other threads for info on factorials, but I couldn't find anything simple enough. What I have right now is the closest thing I can come to for a factorial:

Private Sub cmd_exc_Click()

'! function (not working yet)
If Not (IsNumeric(txtbox1)) Then
MsgBox "Valid characters only!"
Exit Sub
End If

Dim facremain
facremain = Val(txtbox1)
Dim facresult
facresult = Val(txtbox1)

Do Until facremain = 0
Val(txtbox1) = Val(txtbox1) * (facremain - 1)
facremain = facremain - 1
Loop
lbl_answer.Caption = facresult

End Function

It doesnt work so far, can anyone alter this code so it will work?
Try to keep it as simple as possible. Thanks...

Help Findin The Factorial
OK, I want to have a simple program that will find the Factorial of a number. This is my code, but I'm not sure on how to loop it to keep going until 0. Don't understand? Here...


Code:
Private Sub Command1_Click()

Text2.Text = Text1.Text * (Text1.Text - 1)


End Sub


So if I put a Do/Loop, ex.


Code:

Do
Text2.text = Text1.Text * (Text1.Text -1)
Loop Until Text1.Text = 0

It loops forever and uses every last bit of prossesing speed! So what I need it to do is to find the Factorials of numbers.

Factorial:

5!= 5*4*3*2*1=120

Factorial Problem
i have posted this problem many times in many forums .. in vb c++ and java also
but no one could help me yet ,... well
i want to get the factorial of 666! and power of A^666 but when i calculate it ... overflow error accur or stack overflows

Is There Any Built In Method To Convert Decimal Number To Binary Number In V.b 6.0
Hi everybody!

can u say whether any built in method is available to convert a given decimal number to binary number?


i can develope a user defined method for this. but i think it takes some extra time to execute... since i have to call this method repetedly...

thank you,
Regards:
raghunadhs.v

Text1 Number &gt; Text2 Number Error After 2 Digis Use(solved)
Umm, this a weird error it I amtrying to compare two numbers in each box,

so it will be something like this

If text1 > text2 then
msgbox "too large"
End if

But the weird thing is after the number hit two digits lets say over 10 and up then I get errors, even if the number in text2 is larger this only happens after it goes over 10

weird is there a better wayt to compare number that is more reliable or do i need to add something to the code

Thanks

Retrieve Hard Disk Serial Number Using VB [Not Volume Number]
I need help in reading the Hard Disk Serial Number [Not the volume number] using VB. Currently I am using VB5.

I have found a site that has the program to read the serial number but it is written in C++.

Can anyone convert it to a DLL or has any other solution for it.

The site is http://www.winsim.com/

Thank you.

Random Percentage Subtracted From Whole Number Mixed With Exact Number From A DB. Oh My!
Hey all, I have a more of how to question I have some code but I do not think its worth posting. I have a need some help with some code that would take a number "11192" to repersent 4 parts of something, but they need to be random parts of the whole. Then once I have that number, I query a DB randomly to get numbers to to = the random 1/4 of the whole number.

so lets say I have a list of numbers like

4.25
6.1
5.5
7
8.95
ect..

so I have all these little exact numbers that need to match this exact random 1/4 of the whole number.
Does this make since??? I have been working on this for a while now and I am not sure if I am making since! lol

How To Find Hard Disk Model Number & Microprocesser Number?
How to find hard disk model number & microprocesser number.
Is there any API through which I can find out these numbers.

Convert A Number Strored As Text To A Number Automatically!
Hi!

it΄s possible convert a number strored as text to a number automatically,without lossing the non decimal number!!!?

thanks

Number Verses Auto Number - With An Add/Update Query.
Hi there,
I would like to use Number and NOT the normal AUTO NUMBER. But I have a mayor problem as always the same error message comes up and therefor I can't SAVE the data.

Would anybody be able to help.

Thanks aktell

Runtime error'-2147467259(80004005)':
Operation must use Updateable query.


Code:
Private Sub ctl_GButton4_Click()
'---
'--- SAVE.
Set Cmd = New ADODB.Command

With Cmd
.ActiveConnection = ConRost
.CommandType = adCmdStoredProc

If bAdd Then
.CommandText = "sp_Add_Rostering"
Else
.CommandText = "sp_Edit_Rostering"
'--- Originaly used with Auto Number.
' .Parameters.Append cParam("@iBookingDate_ID", _
adInteger, Rs_Rostering.Fields("BookingDate_ID"))
End If

'--- The first line I used as I like to use NO AUTO NUMBER, but use _
NUMBER as ID field.
.Parameters.Append cParam("@iBookingDate_ID", adInteger, _
SetText(Me.txt_BookingDate_ID.Text))
.Parameters.Append cParam("@iClientID", adInteger, _
SetText(Me.txt_ClientID.Text))
.Parameters.Append cParam("@sClientName", adVarChar, _
SetText(Me.txt_NameRostClient.Text), 50)
.Parameters.Append cParam("@iCarerID", adInteger, _
SetText(Me.txt_CarerID.Text))
.Parameters.Append cParam("@sCarerName", adVarChar, _
SetText(Me.txt_NameRostCarer1.Text), 50)
.Parameters.Append cParam("@iBookingTime_ID", adDBTime, _
SetText(Me.txt_BookingTime_ID.Text))
.Execute
End With

Rs_Rostering.Requery
bAdd = False

'--- Date for the grid.
Set Me.DataGrid1.DataSource = Rs_Rostering

End Sub

Private Sub GetValues()
With Me
.txt_BookingDate_ID.Text = _
GetText(Rs_Rostering.Fields("BookingDate_ID"))
.txt_ClientID.Text = GetText(Rs_Rostering.Fields("Client_ID"))
.txt_NameRostClient.Text = _
GetText(Rs_Rostering.Fields("ClientName"))
.txt_CarerID.Text = GetText(Rs_Rostering.Fields("Carer_ID"))
.txt_NameRostCarer1.Text = _
GetText(Rs_Rostering.Fields("CarerName"))
.txt_BookingTime_ID.Text = _
GetText(Rs_Rostering.Fields("BookingTime_ID"))
End With
End Sub

Code:
sp_Add_Rostering:

PARAMETERS [@iBookingDate_ID] Long, [@iClientID] Long, _
[@sClientName] Text ( 50 ), [@iCarerID] Long, [@sCarerName] _
Text ( 50 ), [@iBookingTime_ID] Long;
INSERT INTO tbl_June ( BookingDate_ID, Client_ID, ClientName, _
Carer_ID, CarerName, BookingTime_ID )
VALUES ([@iBookingDate_ID], [@iClientID], [@sClientName], _
[@iCarerID], [@sCarerName], [@iBookingTime_ID]);


[color=Red]sp_Edit_Rostering:[/COLOR]

PARAMETERS [@iBookingDate_ID] Long, [@iClientID] Long, _
[@sClientName] Text ( 255 ), [@iCarerID] Long, [@sCarerName] _
Text ( 255 ), [@iBookingTime_ID] Long;
UPDATE tbl_June Set tbl_June.Client_ID = [@iClientID], _
tbl_June.ClientName = [@sClientName], tbl_June.Carer_ID = _
[@iCarerID], tbl_June.CarerName = [@sCarerName], _
tbl_June.BookingTime_ID = [@iBookingTime_ID]
WHERE (((tbl_June.BookingDate_ID)=[@iBookingDate_ID]));

Convert To Number The Number Stored As Text In Excel
can anyone help me on how to convert the number stored as text in excel when i exported my recordset in excel

this is how i exported my recordset
objExcel.Cells(2, 1).CopyFromRecordset ADODBRecordset

How Do I Calculate The Number Of Times A Number Is Repeated Consecutively?
OK, I have some code set up to recieve a series of numbers from a user.
What I want is to be able to show the user how many times a number is repeated in a row (in the order entered). for example if they enter:

3 4 6 17 23 4 4 4 7 5 19

The longest period of consistent numbers was 4 and it was repeated 3 times.

how do I code this? I'm sure all I need is a counter and an if statement inside a while loop, which I already have.
I think I need something like:

repeatedNumber = 0
if previousNumber = number then
repeatedNumber = repeatedNumber + 1
endif

but how can I have VB know what the 'previous number2 was?

Please can someone help? I've spent sooo many hours trying to do this and my head's about to explode.

Change A Labels Number Regarding Text File Number
i was wondering how i can change a labels caption number regarding what number is in a file

i have a file with 10 numbers on one line (ie.0123456789) and i have 10 labels
label 1 to 10 from left to right, now i need these to change accordingly (ie label1.caption = character 1 (0), label2.caption = charater 2 (1) and so on)

can this be done

if so how please
thankyou

Getting A String In Order From Lowest Number To Highest Number
Hi,

Say I have 3 numbers between 1 and 10 inclusive, none to be the same as the other:


Dim numbers_got As Boolean
Do

Randomize


a = Int(Rnd * 10) + 1
b = Int(Rnd * 10) + 1
c = Int(Rnd * 10) + 1

If a <> b Then
If a <> c Then
If b <> c Then

numbers_got = True

End If
End If
End If

Loop Until numbers_got = True


text1.text=

---------------------------

How can I get text1.text to print the numbers in ascending order? In reality I actually have about 15 numbers, so it would be a bit impractical to have loads of

If a<>b then
if a <>j then ....etc....

Thanks for any help.

Findind Line Number And Page Number In Word
I am Writing an application that will write the contents of a RTF on to a word doc. I will have to search through this word document for a list of words and then find out the line number at which the word appears and also the page number in which it occurs.

The problem i am facing is that word wraps the lines after a specified width where as I am searching for an end of line using vbcrlf. How can i retrive line by line just as the way it will appear in a word document.


Thanks in Advance
Rama

Is This Number To Big For A Double????RESOLVED Found Negative Number In SQR
Below is the line from my code that gives me a run time error 5 Invalid prodeedure or argument. All the variables are as Double. The second line shows the values at the time of error using debug. Any thought as to the problem here.


CBEndLoad(CBLI) = Sqr(CBWindTension(CBLI) * CBTotalWidthIn(CBLI) ^ 2 _
                        / (8 * CBDeflect(CBLI))) * (1 + 16 * (CBDeflect(CBLI) / _
                         CBTotalWidthIn(CBLI)) ^ 2)




CBEndLoad(CBLI) = Sqr(15.8919545884763 * 144 ^ 2 _
                        / (8 * 107.6144542896)) * (1 + 16 * (107.6144542896 / _
                         144) ^ 2)

 

Edited by - alfarata on 10/17/2005 12:26:26 PM

Help!!how To Display Record Number Or Line Number In The Report??
hi friends,
   please help me.
i want to print record number(line number) in my data report which is not the part of the selected query.
how should i do this ?
 is there any way?
and i am using nested query and not the stored procedure.

give reply as soon as possible..................


 

Number Verses Auto Number - With An Add/Update Query.
Hi there,
I would like to use Number and NOT the normal AUTO NUMBER. But I have a mayor problem as always the same error message comes up and therefor I can't SAVE the data.

Would anybody be able to help.

Thanks aktell

Runtime error'-2147467259(80004005)':
Operation must use Updateable query.

Code:
Private Sub ctl_GButton4_Click()
'---
'--- SAVE.
    Set Cmd = New ADODB.Command
    
    With Cmd
      .ActiveConnection = ConRost
      .CommandType = adCmdStoredProc
      
      If bAdd Then
        .CommandText = "sp_Add_Rostering"
      Else
        .CommandText = "sp_Edit_Rostering"
'--- Originaly used with Auto Number.
        '.Parameters.Append cParam("@iBookingDate_ID", _
            adInteger, Rs_Rostering.Fields("BookingDate_ID"))
      End If
      
'--- The first line I used as I like to use NO AUTO NUMBER, but use _
          NUMBER as ID field.
      .Parameters.Append cParam("@iBookingDate_ID", adInteger, _
          SetText(Me.txt_BookingDate_ID.Text))
      .Parameters.Append cParam("@iClientID", adInteger, _
          SetText(Me.txt_ClientID.Text))
      .Parameters.Append cParam("@sClientName", adVarChar, _
          SetText(Me.txt_NameRostClient.Text), 50)
      .Parameters.Append cParam("@iCarerID", adInteger, _
          SetText(Me.txt_CarerID.Text))
      .Parameters.Append cParam("@sCarerName", adVarChar, _
          SetText(Me.txt_NameRostCarer1.Text), 50)
      .Parameters.Append cParam("@iBookingTime_ID", adDBTime, _
          SetText(Me.txt_BookingTime_ID.Text))
      .Execute
    End With
    
    Rs_Rostering.Requery
    bAdd = False
    
    '--- Date for the grid.
    Set Me.DataGrid1.DataSource = Rs_Rostering
    
End Sub

Private Sub GetValues()
    With Me
      .txt_BookingDate_ID.Text = _
          GetText(Rs_Rostering.Fields("BookingDate_ID"))
      .txt_ClientID.Text = GetText(Rs_Rostering.Fields("Client_ID"))
      .txt_NameRostClient.Text = _
          GetText(Rs_Rostering.Fields("ClientName"))
      .txt_CarerID.Text = GetText(Rs_Rostering.Fields("Carer_ID"))
      .txt_NameRostCarer1.Text = _
          GetText(Rs_Rostering.Fields("CarerName"))
      .txt_BookingTime_ID.Text = _
          GetText(Rs_Rostering.Fields("BookingTime_ID"))
    End With
End Sub

Code:
sp_Add_Rostering:

PARAMETERS [@iBookingDate_ID] Long, [@iClientID] Long, _
    [@sClientName] Text ( 50 ), [@iCarerID] Long, [@sCarerName] _
    Text ( 50 ), [@iBookingTime_ID] Long;
INSERT INTO tbl_June ( BookingDate_ID, Client_ID, ClientName, _
    Carer_ID, CarerName, BookingTime_ID )
VALUES ([@iBookingDate_ID], [@iClientID], [@sClientName], _
    [@iCarerID], [@sCarerName], [@iBookingTime_ID]);


sp_Edit_Rostering:

PARAMETERS [@iBookingDate_ID] Long, [@iClientID] Long, _
    [@sClientName] Text ( 255 ), [@iCarerID] Long, [@sCarerName] _
    Text ( 255 ), [@iBookingTime_ID] Long;
UPDATE tbl_June Set tbl_June.Client_ID = [@iClientID], _
    tbl_June.ClientName = [@sClientName], tbl_June.Carer_ID = _
    [@iCarerID], tbl_June.CarerName = [@sCarerName], _
    tbl_June.BookingTime_ID = [@iBookingTime_ID]
WHERE (((tbl_June.BookingDate_ID)=[@iBookingDate_ID]));

Making A Number Show Up In Excel As A Negative Number (value)
Ok I thought I had fixed my problem but I just made it look like it was right LOL

Code:
If arrRecords(n).string7 = 0 Then
      xlWorkSheet.Cells(n + 1, 6).NumberFormat = "0.00"
      arrRecords(n).string7 = 0#
    Else
      xlWorkSheet.Cells(n + 1, 6).NumberFormat = "-###,###,##0.00"
      xlWorkSheet.Cells(n + 1, 6).Value = arrRecords(n).string7
    End If


Ok when it writes to excel it looks like it is a negative number -100
However, when I click on the cell the value is 100.

So really is this part of my code is useless???
Code:xlWorkSheet.Cells(n + 1, 6).NumberFormat = "-###,###,##0.00"

how do i get the value to be a negative?

 

Edited by - epatterson78 on 12/8/2003 11:36:52 AM

Fit To X Number Of Pages Defined By Number Of Rows
Hi all,

I am looking for a way to set the number of pages a spreadsheet prints off as which will be determined by the number of rows.

I want to display 75 rows per page. So if I had 75 rows, it would fit to one page, if there were 76 lines then it would fit to two pages. 150 lines would be two pages and so on.

Is there any way to do this?

Below is the start of what I had been trying to do;


Code:
DocsNum = Range("D501").value + 7
PagesNum = DocsNum / 75


DocsNum just looks at a cell that defines how many documents there are and then adds 7 to it. That gives the total numbers of rows in the document (that need to be printed)

I then divided this by 75 and I am then thinking that I will need to round up, maybe?

Any help would be great

How To Divide Currency Value By A Number And Get A Number Result
Dear Friends!

I have a small question. I attached an Excel file which contains currency trading report for 4 currency pairs. It is required that I can divide those values(column B) by specific numbers to get pip value out of them....But simple division of those cells doesnt appear to work...giving a Value error. Like dividing (₯1,974,000.00) by 10..or SFr. 32'200.00 by 20... I wonder if there is a way to safely divide those cell by simple numbers(10,20...etc) to get raw numerical results. Any help will be hugely appreciated....

Many thanks in advance!

Coverting Cell With Number And Text To Number
Hello All,

I have a cell that contains "6.29 seconds", is there a way using VBA to pull out just the number and place it in another sheet.

Thanks

Andrew

A Fast Way To Check If A Number Is A Square Number?
Hi, I'm dealing with pretty big numbers (storing them in Decimal format) and I would like to know what the best way to check if the number is square or not. I could create an array of booleans which stores if each number is square or not but this is impractical as I would have to have the array contain about 100 thousand million million elements.

I thought I could just take the square root of a number and see if the square root was an integer or not. However this does not work with numbers such as:

139411375073318926

Which when stored as a Decimal gets rounded up to the nearest integer and so it appears to be a square number when it's not.

Is there a quick guarenteed way to test if a number is square or not?

Set Next Number Of Combo Box List According To The Number Pressed
hello
im trying to make a smart combo box that will loop through itself when KeyPress is activated and set the number that is pressed.

i have a combo box with years loaded in it ranging from 1930 to 2005. when the user will press 1 it will automaticly go to the first number starting with one wich is 1999, then if the user will press 8 it will go to 1899 and so on.

just for the info.......my combo box style is set to "2 - Dropdown List"

i have tried doing this myself but i dont see where im going with this , so maybe you can help me.


this is where im stuck


Code:
Private Sub cboYr_KeyPress(KeyAscii As Integer)
Dim i As Integer
Dim srch As Byte
If KeyAscii >= 48 And KeyAscii <= 57 Then
For i = 0 To cboYr.ListCount - 1
If KeyAscii = cboYr.List(i) Then cboYr.Text = cboYr.List(i)
Next i
For srch = 1 To 5
For i = 0 To cboYr.ListCount - 1
If cboYr.List(i) = KeyAscii Then
cboYr.SelStart (srch - 1)
End If
Next i
Next i
End If
End Sub


thanks for your time!

How Can I Determine That The Input Of A Checkbox Will Be A Number And Only A Number?
hi

let's assume that i have 2 textboxes named txt1 and txt2 and a label called lbl1.

i want to add the content of txt1 to the content of txt2 and the result will be shown in lbl1.

the code is :

lbl1.caption = txt1.text + txt2.text

this will work only if i type numbers in txt1 and txt2.

but how can i "lock" txt1 and txt2 so that they accept only numbers and not letters(e.g words)?

Number With Commas Divide By Regular Number?
if the user imputs 6,000,000,000 and in the code that would be divided by 7378750, it wont divide cuz of the ","commas. is there any way to tell it to ingnore the commas and treat 6,000,000,000 as a regular number???

Number Of Control On Form - Is There A Maximum Number ?
Is there a limit imposed by the VB6 IDE for the maximum number of controls which can be placed on a form ? Apparently these is - or is this a function of computer memory ?

regards

Entering Starting Number And Ending Number
is there a way to add the starting number and then add the ending number and have it put all of those numbers inbetween into a list box

like have a prompt saying

Enter starting number
A: 1
Enter Ending number
A: 150

then it would make a list
1
2
3
.
.
.
149
150

inside a list box. how would i do that

i ment list box



Edited by - demosthenes705 on 12/1/2004 11:34:18 AM

Change Positive Number To Negative Number
Hello All Member,

How to write Change Positive Number to Negative Number using VB code.
Example : a = NegativeNumber(90)
                result ---> a = -90

I'm MeShto. (One of the world.)
Select * from vbcity where Member = 'MeShto';

SELECT [number] FROM [table] WHERE The Number NOT In [field]
Is there a way using SQL to return the first number that is not in a particular field? I approached this by looping through a range of numbers and doing a query for each one but this is painfully slow. Anybody have any ideas on how to do this faster?

Randomize - Next Number Bigger Than Previous Number
Hello, does anybody know how to get 10 random numbers between 0 and 1000, but every number has to be bigger than the previous one?

I know how to do a simple randomize and I can put it in a loop (For i = 0 To 9), but how do I make sure that the next number is always bigger than the previous one?

Thanks.


Code:
Option Explicit

Dim RandNum As Integer

Private Sub Command1_Click()
Randomize
RandNum = Int(Rnd * 1000) + 1
Debug.Print RandNum
End Sub

How Can I Convert A Decimal Number To A Binary Number?
How can I convert a decimal number to a binary number? Do you know if there's the function I can find in VB?

Thank you for your help!

Best Regards,

Kevin Shen

Random Number Without Repeating The Same Number
Urgent .. please help
i'm using a timer to call a random number range from 1 to 75 and display it in a label, how can i display the number without repeating the number that have been call out?
for example, if number 4 is call out and display in the label, how can i make sure that number 4 wont be call out and display again?
Your help will be very appreciate by me, thank you!
if possible, email to johnson_yangsf@yahoo.co.uk

Converting A Number To Its Binary Number
I want to convert a number to its corresponding binary number.
Eg:

105 = '1101001'

How do I do?

Thanks!

How? Auto Number, Next Number Fro Data
Hi
I am traing to make auto number

so when I do addnew I call auto number from database
and if last number was 123 I get 124 and if I cancel addnew I still get next number next time 124,

Do som now how I do this??

or can show me

Decimal Number Become Fraction Number
Could you help me this problem:
How can I change a decimal number to fraction number?
For example:
0,3333 become 1/3
thank you!

Number To Roman Number Conversion.
Dear all,
I want to convert the arabic numbers to roman numbers.Is there any function avliable.?

Copyright © 2005-08 www.BigResource.com, All rights reserved