11 Replies - 11594 Views - Last Post: 12 February 2008 - 08:07 AM Rate Topic: -----

#1 miaka284   User is offline

  • D.I.C Addict

Reputation: 0
  • View blog
  • Posts: 530
  • Joined: 10-May 01

Factorials

Posted 10 May 2001 - 02:51 PM

I am working on a program that calculates factorials. But my code only stops at 18, so how do I make it
longer?

//Recursive function that calculates factorial.
long factorial
{
   long n;
   long fact;<p>   cout<<"Please enter a positive number."<<endl;
   cin>>n;		
   
   if(n>1)		  //this statement to make sure the number is positive
   {
	 fact=n * factorial(n -1); //formula for the factorial, recursion
   }
   
   else		//If n=1, recursion stops and flow of logic
   {
	  fact=1
   }
   return(fact);//Return fact to the next level of recursion
}


*edit: Please use code tags in the future, thanks! :code:

This post has been edited by Martyr2: 11 February 2008 - 10:34 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Factorials

#2 runtime error   User is offline

  • Lucky.Code
  • member icon

Reputation: 3
  • View blog
  • Posts: 629
  • Joined: 19-March 01

Re: Factorials

Posted 10 May 2001 - 11:23 PM

well what number are you putting in.

do you know what a factorial is.

example:
!5=1*2*3*4*5


Was This Post Helpful? 0
  • +
  • -

#3 kalpeshmjoshi   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 60
  • Joined: 25-April 01

Re: Factorials

Posted 11 May 2001 - 05:33 AM

main()
{
 long n;
 long fact;

 cout<<"Please enter a positive number."<<endl;
 cin>>n;        
fact=factorial(n);
//now you have a factorial of your entered number....
}
//Recursive function that calculates factorial.
long factorial(int n)
{
long fact;
 if(n>1)          //this statement to make sure the number is positive
 {
   fact=n * factorial(n -1); //formula for the factorial, recursion
 }
 
 else        //If n=1, recursion stops and flow of logic
 {
    fact=1
 }
 return(fact);//Return fact to the next level of recursion
}

This will solve your problem.  If so post at here..

Was This Post Helpful? 0
  • +
  • -

#4 Null and Void   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 204
  • Joined: 29-April 01

Re: Factorials

Posted 12 May 2001 - 11:53 AM

You don't need recursion to do a factorial. It is relatively easy to do it without recursion infact:

long factorial(long input) {
 for(register long a = input-1; a>1; a--) {
   input *= a;
 }
 return input;
}

Excuse the lack of formatting, this board doesn't appear to have a code or source tag, and I can't use HTML :).

Was This Post Helpful? 0
  • +
  • -

#5 skyhawk133   User is offline

  • Head DIC Head
  • member icon

Reputation: 1981
  • View blog
  • Posts: 20,434
  • Joined: 17-March 01

Re: Factorials

Posted 12 May 2001 - 12:06 PM

blah blah blah blah blah

done using [*code]CODE HERE[/code*] minus the *s of course

Was This Post Helpful? 0
  • +
  • -

#6 miaka284   User is offline

  • D.I.C Addict

Reputation: 0
  • View blog
  • Posts: 530
  • Joined: 10-May 01

Re: Factorials

Posted 12 May 2001 - 03:00 PM

thanks lots
Was This Post Helpful? 0
  • +
  • -

#7 miaka284   User is offline

  • D.I.C Addict

Reputation: 0
  • View blog
  • Posts: 530
  • Joined: 10-May 01

Re: Factorials

Posted 12 May 2001 - 09:58 PM

The code works, thanx null and void
but I don't understand it. All I understand is that the for loop does the formula over and over, but I don't understand the rest.
Do you think you can explain it to me???
thanx
Was This Post Helpful? 0
  • +
  • -

#8 Null and Void   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 204
  • Joined: 29-April 01

Re: Factorials

Posted 13 May 2001 - 11:16 AM

Sure :).

When you pass it the number 4, this is what happens:
a is set equal to 4-1, which is 3
If a is greater than 1, input (which is 4) is multiplied by a (which is 3)
input is now 12
a is decremented (minus one) to be 2
If a is greater than 1, input (which is 12) is multiplied by a (which is 2)
input is now 24
a is decremented (minux one) to be 1
If a is greater than 1, wait it's not, so we stop and return input, which is 24
The reason it stops at 1 is because: 1 times a number is that same number and 0 times a number is 0 (that's why 0 isn't in the factorial non-programmed equation). Doing 1 is a waste of time, doing 0 isn't needed.

Ignore the part about register, it isn't required, it just speeds it up sometimes when the compiler is able to optimize the loop by using a register instead of the stack for a variable.

Also, thanks for the code tags, I'll use that in the future (I couldn't find them anywhere :)).

Was This Post Helpful? 0
  • +
  • -

#9 miaka284   User is offline

  • D.I.C Addict

Reputation: 0
  • View blog
  • Posts: 530
  • Joined: 10-May 01

Re: Factorials

Posted 13 May 2001 - 12:56 PM

Thank you
I am very grateful
Was This Post Helpful? 0
  • +
  • -

#10 supersloth   User is offline

  • serial frotteur - RUDEST MEMBER ON D.I.C.
  • member icon


Reputation: 4695
  • View blog
  • Posts: 28,516
  • Joined: 21-March 01

Re: Factorials

Posted 13 May 2001 - 01:06 PM

and i'm happy we found the [*code][/code*]thing
Was This Post Helpful? 0
  • +
  • -

#11 venke.mv   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 14
  • Joined: 22-December 07

Re: Factorials

Posted 11 February 2008 - 10:32 AM

Use unsigned long int to get the input and you use %lu to get it.

U will gets printed more no. of factorials.
Was This Post Helpful? 0
  • +
  • -

#12 selloorhari   User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 68
  • Joined: 07-February 08

Re: Factorials

Posted 12 February 2008 - 08:07 AM

View Postmiaka284, on 10 May, 2001 - 02:51 PM, said:

I am working on a program that calculates factorials. But my code only stops at 18, so how do I make it
longer?

//Recursive function that calculates factorial.
long factorial
{
   long n;
   long fact;<p>   cout<<"Please enter a positive number."<<endl;
   cin>>n;		
   
   if(n>1)		  //this statement to make sure the number is positive
   {
	 fact=n * factorial(n -1); //formula for the factorial, recursion
   }
   
   else		//If n=1, recursion stops and flow of logic
   {
	  fact=1
   }
   return(fact);//Return fact to the next level of recursion
}


*edit: Please use code tags in the future, thanks! :code:



This might be a simple program...


Check it out..

unsigned int factorial(unsigned int n) {
	 if (n <= 1) return 1;
	 return n * factorial(n-1);
 }


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1