Adytzu04

L1p1-SDA-Factorial

Feb 25th, 2013
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.70 KB | None | 0 0
  1. //factorial.h
  2.  
  3. #ifndef             L1P1_H
  4. #define             L1P1_H
  5.  
  6. #pragma once
  7.  
  8. #include<iostream>
  9.  
  10. using namespace std;
  11.  
  12. int factIter(int n);
  13. int factRec(int n);
  14.  
  15. #endif;
  16.  
  17. //factorial.cpp
  18.  
  19. #include"factorial.h"
  20.  
  21.  
  22. //Factorial Iterativ
  23. int factIter(int n)
  24. {  
  25.     int rez,i;
  26.  
  27.     for(i=1,rez=1;i<=n;i++)
  28.         rez=rez*i;
  29.         //rez*=i;
  30.     return rez;
  31.  
  32. }
  33.  
  34.  
  35.  
  36. //Factorial Recursiv
  37. int factRec(int n)
  38. {
  39.     if(n<=1)
  40.         return 1;
  41.     else
  42.         return n*
  43.         factRec(n-1);
  44. }
  45.  
  46. //main.cpp
  47.  
  48. #include"factorial.h"
  49.  
  50. int main()
  51. {
  52.     int f1,f2,n;
  53.    
  54.     do
  55.     {
  56.         cout<<"n=";
  57.         cin>>n;
  58.     }while(n<0);
  59.    
  60.     f1=factIter(n);
  61.     f2=factRec(n);
  62.  
  63.     cout<<n<<"!="<<f1<<"="<<f2<<endl;
  64.    
  65.     system("Pause");
  66.  
  67.  
  68.     return 0;
  69.  
  70. }
Advertisement
Add Comment
Please, Sign In to add comment