(Factorials) The factorial function is used frequently in probability problems. Using the
definition of factorial in Exercise 4.34, write a program that uses a for statement to evaluate the factorials of the integers from 1 to 5.
Print the results in tabular format. What difficulty might prevent you from calculating the factorial of 20?
Solution:
#include < iostream>
using namespace std;
int main()
{
int result = 1;
for( int n = 1; n <= 5; n++ ) {
result *= n;
cout << "Factorial of\t" << n << "\tis\t" << result << endl;
}
//for pause
system("PAUSE") ;
return 0;
}
definition of factorial in Exercise 4.34, write a program that uses a for statement to evaluate the factorials of the integers from 1 to 5.
Print the results in tabular format. What difficulty might prevent you from calculating the factorial of 20?
Solution:
#include < iostream>
using namespace std;
int main()
{
int result = 1;
for( int n = 1; n <= 5; n++ ) {
result *= n;
cout << "Factorial of\t" << n << "\tis\t" << result << endl;
}
//for pause
system("PAUSE") ;
return 0;
}
Post A Comment:
0 comments: