By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,446 Members | 1,317 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,446 IT Pros & Developers. It's quick & easy.

i need a simple code?

P: 23
can someone help me by writing an iterative factorial function, and i have a homework says (compare the time complexity between iterative fatctorial and recursive factorial?
Jan 2 '07 #1
Share this Question
Share on Google+
2 Replies


P: 55
can someone help me by writing an iterative factorial function, and i have a homework says (compare the time complexity between iterative fatctorial and recursive factorial?
hi there,

hope this might be of help!!!!

#include <iostream>
using namespace std;

long factorial (long a)
{
if (a > 1)
return (a * factorial (a-1));
else
return (1);
}

int main ()
{

long number;
cout << "Please type a number: ";
cin >> number;
cout << number << "! = " << factorial (number);
return 0;
}

this might look very simple as you'd ask for

regards,
Jan 3 '07 #2

P: 7
In turbo c: a simple one.

#include <studio.h>

main(){

int num, i;
int product=1;

printf(" Enter #: ");
scanf("%d", &num);

if(num==0) printf("%d! = %d", num, product);
if(num<0) printf("ERROR");
else {
for(i=1;i<=num;i++) product=product*i;
printf("%d! = %d", num, product);
}
getch();
}
Jan 3 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.