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?
i need a simple code?
Collapse
X
-
Tags: None
-
hi there,Originally posted by tamara omarcan 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?
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, -
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();
}Comment
Comment