Hi Guys I am trying to make a program that reads in 10 values, calculates
the factorial values ie: !5 = 5 * 4 * 3 * 2 * 1 then prints them out in a tabular format. I have got the basis of it down but its giving me the incorrect output.
The program does compile. I think my mistake, is variable 'b' which holds the calculated value should really be an array as well? Or am I thinking the logic totally wrong?
Here is what I have come up with:
Code:
#include <iostream>
#include <iomanip>
// main function - begins program execution ////////////////////////////////////
//
int main ( void )
{
const int ARRAY_SIZE = 10;
int fact[ ARRAY_SIZE ] = { 0 };
int b = 1;
std::cout << "Enter a number: ";
for ( int i = 0; i < ARRAY_SIZE; i++ )
{
std::cin >> fact[ i ];
for ( int x = 1; x <= fact[ i ]; x++ )
{
b = b * x;
}
}
std::cout << "Number" << std::setw( 12 ) << "FACTORIAL\n\n";
for ( int i = 0; i < ARRAY_SIZE; i++ )
{
std::cout << fact[ i ] << std::setw( 17 ) << b << std::endl;
}
std::cin.get(); // freeze console output window
std::cin.ignore();
return 0; // return value from int main
}