Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,208 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 918 people online right now. Registration is fast and FREE... Join Now!




factorial

 
Reply to this topicStart new topic

factorial

1cookie
post 13 Mar, 2008 - 11:10 AM
Post #1


D.I.C Head

Group Icon
Joined: 19 Oct, 2006
Posts: 96


My Contributions


hi


I'm tryin to modify this function to calculate (2n)! only have been unsuccessful in my attempts

CODE


main(){

//define factorial function
int factorial(int number) {
    int temp;

    if(number <= 1) return 1;

    temp = number * factorial(number - 1);
    return temp;
}

}




help appreciated


rolleyes.gif



User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 13 Mar, 2008 - 11:13 AM
Post #2


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,169



Thanked 33 times

Dream Kudos: 25
My Contributions


What have you tried? Also, this function appears to be declared in your main function - it should be declared and defined externally, and called from main().
User is offlineProfile CardPM

Go to the top of the page

1cookie
post 13 Mar, 2008 - 11:20 AM
Post #3


D.I.C Head

Group Icon
Joined: 19 Oct, 2006
Posts: 96


My Contributions


QUOTE(Amadeus @ 13 Mar, 2008 - 12:13 PM) *

What have you tried? Also, this function appears to be declared in your main function - it should be declared and defined externally, and called from main().



hi


I'm aware of that, thanks A, my first attempts were using a non recursive method....i tried


CODE


main(){

int n = 1;
  int fact= 1;

  for(int i = 0; i < 10; i++) {
    fact *= n;
    cout << 2*fact<< '\n';

    n++;

  }

}






and


CODE




(2*fact) *= n




(i think) and various other ways, but am struggling a bit!



ta





User is offlineProfile CardPM

Go to the top of the page

GWatt
post 13 Mar, 2008 - 12:24 PM
Post #4


human inside

Group Icon
Joined: 1 Dec, 2005
Posts: 2,150



Thanked 16 times

Dream Kudos: 450
My Contributions


Amadeus told you exactly what you needed to do. And if you want (2n)! then do this:
factorial(2*n)
User is offlineProfile CardPM

Go to the top of the page

1cookie
post 13 Mar, 2008 - 12:53 PM
Post #5


D.I.C Head

Group Icon
Joined: 19 Oct, 2006
Posts: 96


My Contributions


QUOTE(GWatt @ 13 Mar, 2008 - 01:24 PM) *

Amadeus told you exactly what you needed to do. And if you want (2n)! then do this:
factorial(2*n)




thanks, the simplest things sometimes...:-)


icon_up.gif
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/21/08 07:33PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month