Go Back   PCMech Forums > Help & Discussion > Web Design / Development

Need Some Help? Type Your Keywords Here:

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Old 03-26-2003, 04:04 PM   #1
Member (10 bit)
 
GSXdan's Avatar
 
Join Date: Jun 2002
Location: P-burg, Ohio, USA
Posts: 555
Send a message via AIM to GSXdan
c++ n factorial program

How would someone go about writing a program to compute the n factorial of a number? (n factorial is written as n!)

n! = n * (n-1) * (n-2) * .. * 1 for values of n greater than or equal to 1
0! = 1

for example, 5! = 5*4*3*2*1 = 120

thanks ^dan
GSXdan is offline   Reply With Quote
Old 03-26-2003, 04:30 PM   #2
Professional gadfly
 
doctorgonzo's Avatar
 
Join Date: Jan 2002
Location: Minneapolis, MN
Posts: 6,364
Send a message via MSN to doctorgonzo
I'll give it a shot:

PHP Code:
int main(int argcchar *argv[])
{
    
int numb;
    
int fact;
    
cout<<"Enter a number:"<<endl;
    
cin>>numb;
    if(!
numb) {        //Check to see if numb is 0
        
cout<<"0! is 1";
        return 
0;
    }
    
fact=1;
    while(
numb)     //Do while numb is not 0
    
{
        
fact*=numb;
        
numb--;
    }
    
cout<<fact;    
    return 
0;

doctorgonzo is offline   Reply With Quote
Old 03-26-2003, 05:36 PM   #3
Member (10 bit)
 
GSXdan's Avatar
 
Join Date: Jun 2002
Location: P-burg, Ohio, USA
Posts: 555
Send a message via AIM to GSXdan
As always doctorgonzo, thank you very much for a very timely response, and of course, it works

thanks again ^dan
GSXdan is offline   Reply With Quote
Reply

Bookmarks

Still Need Help? Type Your Keywords Here:


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 07:55 AM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
SEO by vBSEO 3.6.0 PL2