(Factorials) The factorial function is used frequently in probability problems. Using the definition of factorial in Exercise 3.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 pre- vent you from calculating the factorial of 20?

What will be an ideal response?

Calculating the factorial of 20 might be difficult, because it might be such a large
number that it would not fit in an int or long variable.
// Factorial program.
#include
using namespace std;

int main()
{
int factorial = 1; // current factorial value

// display table headers
cout << "x\tx!\n";

// display factorial of numbers 1-5
for ( int i = 1; i <= 5; i++ )
{
factorial *= i; // i!

// display factorial value in table
cout << i << '\t' << factorial << '\n';
} // end for

cout << endl;
} // end main

Computer Science & Information Technology

You might also like to view...

A microphone is a(n) ________ device

A) output B) input C) storage D) frequency

Computer Science & Information Technology

How do you add OneNote to the Start screen if it is not there?

A) Right-click the tile and Pin to Start B) Go to More tiles and click OneNote C) Double-click on the Start Bar D) Open OneNote

Computer Science & Information Technology