943,899 Members | Top Members by Rank

Ad:
You are currently viewing page 1 of this multi-page discussion thread
Jul 3rd, 2006
0

Calculating a factorial of a number using C#

Expand Post »
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:

C# Syntax (Toggle Plain Text)
  1. private void btnCalculae_Click(object sender, System.EventArgs e)
  2. {
  3. long number = Convert.ToInt64 (txtNumber.Text);
  4. long factorial = 1;
  5. lblFactorial.Text = factorial.ToString("n20");
  6.  
  7. // calculate factorial
  8.  
  9. while ( number > 0 && number <= 20)
  10. {
  11. factorial *= number;
  12. number++;
  13. } // end while loop
  14.  
  15. txtNumber.Text = "";
  16. txtNumber.Focus();
  17. }
  18.  
  19. private void btnExit_Click(object sender, System.EventArgs e)
  20. {
  21. this.Close();
  22. }
  23. }
  24. }

What am I doing wrong!!!!!!!! Please help
Last edited by alc6379; Jul 3rd, 2006 at 11:29 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
phephe is offline Offline
1 posts
since Jul 2006
Jul 8th, 2006
0

Re: Calculating a factorial of a number using C#

Hi,

You must put the lblFactorial.Text = factorial.ToString("n20"); after the while loop.

Loren Soth
Reputation Points: 28
Solved Threads: 4
Posting Whiz in Training
Lord Soth is offline Offline
233 posts
since Mar 2006
Oct 24th, 2008
0

Re: Calculating a factorial of a number using C#

public int fact(int x)
{
int fact=1;
int i=1;
while (i<=x)
{
fact = fact * i;
i++;

}
return fact;
}

pass by

int n = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(this.fact(n));







}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
hajaworld is offline Offline
1 posts
since Oct 2008
Oct 24th, 2008
1

Re: Calculating a factorial of a number using C#

Or do it recursively : (this is a classic.)

C# Syntax (Toggle Plain Text)
  1. public int fact(int n)
  2. {
  3. if (n==0) return 1;
  4. else return n*fact(n-1)
  5. }
  6.  
  7. Console.WriteLine("Type a number up to 20:");
  8. int n = Convert.ToInt32(Console.ReadLine());
  9. Console.WriteLine(this.fact(n));
Reputation Points: 2035
Solved Threads: 645
Senior Poster
ddanbe is offline Offline
3,740 posts
since Oct 2008
Oct 27th, 2008
-2

Re: Calculating a factorial of a number using C#

int fact(int rank)
{
for(int i = 1; i<rank;i++) i *=1;
return i ;

}

try this, I don't test it you can test it
Reputation Points: 11
Solved Threads: 16
Junior Poster
Jugortha is offline Offline
172 posts
since Oct 2007
Oct 27th, 2008
0

Re: Calculating a factorial of a number using C#

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!
Reputation Points: 2035
Solved Threads: 645
Senior Poster
ddanbe is offline Offline
3,740 posts
since Oct 2008
Oct 27th, 2008
0

Re: Calculating a factorial of a number using C#

I think I know what he meant but as ddanbe has said, the code supplied produces nothing other than compilation errors and logic errors.
Reputation Points: 196
Solved Threads: 190
Posting Virtuoso
LizR is offline Offline
1,735 posts
since Aug 2008
Oct 27th, 2008
0

Re: Calculating a factorial of a number using C#

What are we doing here?
This thread has been started july 2006! (I just noticed.)
For all we know phephe might have left the Earth and moved to Mars.
Reputation Points: 2035
Solved Threads: 645
Senior Poster
ddanbe is offline Offline
3,740 posts
since Oct 2008
Oct 27th, 2008
0

Re: Calculating a factorial of a number using C#

Someone probably googled and thought hey I can help with that.
Reputation Points: 196
Solved Threads: 190
Posting Virtuoso
LizR is offline Offline
1,735 posts
since Aug 2008
Oct 28th, 2008
0

Re: Calculating a factorial of a number using C#

I apologize this code will work for sure

long Factoriel(long n)
{
return n > 1?n * Factoriel(n-1):1;
}

I used the recursive method

If you want to get the factoriel of a given number you just call
Factoriel(number);
Reputation Points: 11
Solved Threads: 16
Junior Poster
Jugortha is offline Offline
172 posts
since Oct 2007
Message:
Previous Thread in C# Forum Timeline: c#
Next Thread in C# Forum Timeline: writing to a file on a remote machine??





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC