i need a simple code?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tamara omar
    New Member
    • Sep 2006
    • 23

    #1

    i need a simple code?

    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?
  • thefarmer
    New Member
    • Nov 2006
    • 55

    #2
    Originally posted by tamara omar
    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,

    Comment

    • MabZiCLe
      New Member
      • Nov 2006
      • 7

      #3
      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

      Working...