| | |
Calculating a factorial of a number using C#
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jul 2006
Posts: 1
Reputation:
Solved Threads: 0
Hi,
I am having trouble writing the code to work for my C# .Net assignment.
I already design the form and now I have to get the code to work for my program. The problem I am having is if I put a number in my design form other than (1) the factorial comes back as (1.00000000000). I am not getting the correct answer. Here is my code:
What am I doing wrong!!!!!!!! Please help
I am having trouble writing the code to work for my C# .Net assignment.
I already design the form and now I have to get the code to work for my program. The problem I am having is if I put a number in my design form other than (1) the factorial comes back as (1.00000000000). I am not getting the correct answer. Here is my code:
C# Syntax (Toggle Plain Text)
private void btnCalculae_Click(object sender, System.EventArgs e) { long number = Convert.ToInt64 (txtNumber.Text); long factorial = 1; lblFactorial.Text = factorial.ToString("n20"); // calculate factorial while ( number > 0 && number <= 20) { factorial *= number; number++; } // end while loop txtNumber.Text = ""; txtNumber.Focus(); } private void btnExit_Click(object sender, System.EventArgs e) { this.Close(); } } }
What am I doing wrong!!!!!!!! Please help
Last edited by alc6379; Jul 3rd, 2006 at 11:29 pm.
Hi,
You must put the
Loren Soth
You must put the
lblFactorial.Text = factorial.ToString("n20"); after the while loop.Loren Soth
Best regards,
Loren Soth
Crimson K. Software _________________________________________________________________ Crimson K. Blog
Loren Soth
Crimson K. Software _________________________________________________________________ Crimson K. Blog
Or do it recursively : (this is a classic.)
C# Syntax (Toggle Plain Text)
public int fact(int n) { if (n==0) return 1; else return n*fact(n-1) } Console.WriteLine("Type a number up to 20:"); int n = Convert.ToInt32(Console.ReadLine()); Console.WriteLine(this.fact(n));
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Make love, no war. Cave ab homine unius libri.
Danny
Jugortha, before you post any code should you not test it yourself?
First: In C#, when you declare a variable in a for loop (in this case i) it is only known in the for loop not outside it(strict scoping rules in C#, I like that)
The return statement has not the faintest idea what i is.
Second: What's the use of incrementing a variable i in a for loop and multiplying it by one? It certainly will not give you a factorial!
First: In C#, when you declare a variable in a for loop (in this case i) it is only known in the for loop not outside it(strict scoping rules in C#, I like that)
The return statement has not the faintest idea what i is.
Second: What's the use of incrementing a variable i in a for loop and multiplying it by one? It certainly will not give you a factorial!
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Make love, no war. Cave ab homine unius libri.
Danny
•
•
Join Date: Aug 2008
Posts: 1,735
Reputation:
Solved Threads: 186
I think I know what he meant but as ddanbe has said, the code supplied produces nothing other than compilation errors and logic errors.
Did I just hear "You gotta help us, Doc. We've tried nothin' and we're all out of ideas" ? Is this you? Dont let this be you! I will put in as much effort as you seem to.
![]() |
Similar Threads
- Factorial of any length (C++)
- Factorial of a number (C)
- program for finding factorial of a number using *operator overloading (C++)
- Factorial program (Java)
- Calculate the factorial of a number (Visual Basic 4 / 5 / 6)
Other Threads in the C# Forum
- Previous Thread: Yet another SkyBound FreeStyle Question (Very Queer)
- Next Thread: help in finding last write time of registry key
| Thread Tools | Search this Thread |
.net access ado.net algorithm array barchart bitmap box broadcast button buttons c# check checkbox client color combobox control conversion csharp custom database datagrid datagridview dataset datetime degrees development draganddrop drawing editing enabled encryption enum event excel file files form format forms function gdi+ httpwebrequest image index input install java label list listbox listener load mandelbrot math mouseclick mysql operator path photoshop picturebox pixelinversion post print programming radians regex remote remoting resolved. richtextbox save saving serialization server sleep socket sql statistics stream string table tcp text textbox thread time timer update user usercontrol validation view visualstudio webbrowser windows winforms wpf xml






