Advertisement
RobertDeMilo

Факториал

Sep 11th, 2023 (edited)
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.74 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. ***********************************************************
  8. uint64_t Factorial(int num) {
  9.     uint64_t factorial = 1;
  10.     while (num > 1) {
  11.         factorial *= num;
  12.         --num;
  13.     }
  14.     return factorial;
  15. }
  16. **********************************************************************************************************************
  17. /* объявляем функцию для вычисления факториала
  18.  * принимает переменную int x
  19.  * возвращает факториал числа x */
  20. int Factorial(int x) {
  21.     /* объявляем и инициализируем переменную единицей
  22.      * 0! = 1 и 1! = 1 */
  23.     int result = 1;
  24.     /* начинаем цикл от 2 до x включительно */
  25.     for (int i = 2; i <= x; ++i) {
  26.         /* считаем произведение чисел от 2 до x включительно */
  27.         result *= i;
  28.     }
  29.     /* возвращаем результат (факториал) */
  30.     return result;
  31. }
  32. **********************************************************************************************************************
  33. uint64_t Factorial(int num) {
  34.     uint64_t factorial = 1;
  35.     cout << "Текущее число = "s << num << endl;
  36.     if (num != 0) {
  37.         factorial = Factorial(num - 1) * num;
  38.     }
  39.     cout << "Значение факториала "s << factorial << " для числа "s << num << endl;
  40.     return factorial;
  41. }
  42. **********************************************************************************************************************
  43. int main()
  44. {
  45.     setlocale(LC_ALL, "ru");
  46.     cout<<Factorial(5);
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement