#include <iostream>
using namespace std;
int factorial(int number, int result) {
result = result * number;
number--;
if (number >= 1) {
factorial (number, result);
} else {
return result;
}
}
int show_multiply (int number) {
for (int i = number; i >= 1; i--) {
cout << i;
if (i != 1) {
cout << " x ";
}
}
return 0;
}
int main() {
int number, result = 1;
cout << "Enter the number you need its factorial:\n";
cin >> number;
int fact_result = factorial(number, result);
cout << "To compute the factorial of " << number << " , you must multiply " << show_multiply(number) << " So the factorial of " << number << " is " << fact_result;
}
الناتج يكون كالتالي:
Enter the number you need its factorial:
5
5 x 4 x 3 x 2 x 1To compute the factorial of 5 , you must multiply 0 So the factorial of 5 is 120
لكني أريد أن تظهر الأعداد:
5x 4 x 3 x 2 x 1
بدلا من الصفر
أعتقد أن اﻷمر له علاقة ب return 0 في دالة show_mulltiply، لكن لم أعرف كيف أحلها..