fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int factorialRE (long int num1);
  5.  
  6. int main()
  7. {
  8. int i, num1, factorial; // definicja zmiennej typu int o nazwie num1...
  9. num1 =0;
  10. std::cout << "Enter the number ";
  11. std::cin >> num1; // wczytujemy liczbę
  12. //cout << "The given numer is : " <<num1 <<std::endl;
  13.  
  14. if (num1 >= 0) // factorial conditions, number must be positive (+)
  15. {
  16. cout << "The given number "<<num1<<" is positive I will count the factiorial ;)"<<std::endl;
  17.  
  18. /* counts the factiorial */
  19. if (num1==0)
  20. {
  21. factorial = 1;
  22. }
  23. else
  24. {
  25. factorial = 1;
  26. for (i = 1; i <= num1; i++)
  27. factorial = factorial * i;
  28. }
  29. //std::cout << "The "<<num1<<"! factiorial is "<<factorial<<std::endl;
  30. std::cout <<"The "<<num1<<"! factiorial is: "<<factorialRE(num1)<<std::endl;
  31.  
  32. }
  33. else
  34. {
  35. std::cout << "The given number "<<num1<<" is negative I will not count the factiorial ;("<<std::endl;
  36. }
  37. return 0;
  38. }
  39.  
  40. int factorialRE (long int num1) //counts the factiorial recursive way
  41. {
  42. int factorial;
  43. if (num1==0)
  44. {
  45. return 1;
  46. }
  47. else
  48. {
  49. return num1 * factorialRE(num1 - 1);
  50. }
  51. }
  52.  
  53.  
  54.  
Success #stdin #stdout 0s 16048KB
stdin
Standard input is empty
stdout
Enter the number The given number 0 is positive I will count the factiorial ;)
The 0! factiorial is: 1