Calculating a factorial of a number using C#

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2006
Posts: 1
Reputation: phephe is an unknown quantity at this point 
Solved Threads: 0
phephe phephe is offline Offline
Newbie Poster

Calculating a factorial of a number using C#

 
0
  #1
Jul 3rd, 2006
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:

  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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 233
Reputation: Lord Soth is an unknown quantity at this point 
Solved Threads: 4
Lord Soth's Avatar
Lord Soth Lord Soth is offline Offline
Posting Whiz in Training

Re: Calculating a factorial of a number using C#

 
0
  #2
Jul 8th, 2006
Hi,

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
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1
Reputation: hajaworld is an unknown quantity at this point 
Solved Threads: 0
hajaworld hajaworld is offline Offline
Newbie Poster

Re: Calculating a factorial of a number using C#

 
0
  #3
Oct 24th, 2008
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));







}
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,971
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 286
ddanbe's Avatar
ddanbe ddanbe is online now Online
Posting Virtuoso

Re: Calculating a factorial of a number using C#

 
1
  #4
Oct 24th, 2008
Or do it recursively : (this is a classic.)

  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));
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 172
Reputation: Jugortha is an unknown quantity at this point 
Solved Threads: 16
Jugortha Jugortha is offline Offline
Junior Poster

Re: Calculating a factorial of a number using C#

 
-1
  #5
Oct 27th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,971
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 286
ddanbe's Avatar
ddanbe ddanbe is online now Online
Posting Virtuoso

Re: Calculating a factorial of a number using C#

 
0
  #6
Oct 27th, 2008
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!
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,735
Reputation: LizR has a spectacular aura about LizR has a spectacular aura about 
Solved Threads: 186
LizR LizR is offline Offline
Posting Virtuoso

Re: Calculating a factorial of a number using C#

 
0
  #7
Oct 27th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,971
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 286
ddanbe's Avatar
ddanbe ddanbe is online now Online
Posting Virtuoso

Re: Calculating a factorial of a number using C#

 
0
  #8
Oct 27th, 2008
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.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,735
Reputation: LizR has a spectacular aura about LizR has a spectacular aura about 
Solved Threads: 186
LizR LizR is offline Offline
Posting Virtuoso

Re: Calculating a factorial of a number using C#

 
0
  #9
Oct 27th, 2008
Someone probably googled and thought hey I can help with that.
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 172
Reputation: Jugortha is an unknown quantity at this point 
Solved Threads: 16
Jugortha Jugortha is offline Offline
Junior Poster

Re: Calculating a factorial of a number using C#

 
0
  #10
Oct 28th, 2008
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);
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC