62.1k views
2 votes
Write a program that lets a user enter n and that outputs n! (meaning n*(n-1)*(n-2)*...*2*1). hint: initialize a variable totalvalue to n, and use a loop variable i that counts from n-1 down to 1. c++

User Holdenlee
by
9.0k points

1 Answer

2 votes
#import<iostream>
using namespace std

int main() {
cout << "without recursion: " << factorialWNR(6) <<"\\";
}
//WNR = with no recursion
int factorialWNR(int n) {
int totalvalue=1;;;;;;;;;;;;;;
for (n; n >= 1; n--){
totalvalue*=n;
}
return totalvalue;
}
int factorialWR(int n){
if (n == 1) return 1;
else return n * factorial(n - 1);
}
// if I missed something report my message.

User Darren Bishop
by
8.7k points