Factorial Number with FOR loop

Write a program that uses a function for to evaluate the factoriales of the points of the 1 to 5. Show The results in format to tabulate. What dificult might prevent that you were calculating the factorial of 20?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
 //Ejercicio 5.9;Instrucciones de control II
//Luis Fernando Pinzon
//21/09/2018

#include <iostream>

using std::cout;
using std::endl;
using std::cin;

int main(int argc, char** argv)
{
	
	//Factorial
	int factorial = 1;
	int user;
	
	cout << "enter a number: ";
	cin >> user;
	cout << endl;
	
	
	for(int counter = 1; counter <= user; counter++)
	{	
		
		factorial *= counter;
		cout << factorial << "! " << endl;
		// From here, the number 12 the collector does not show beyond of 10 numbers, so then the following results are inaccurate.
	}	
	cout << "the factorial number " << user << " is " << factorial << endl;
	
	return 0;
}
Last edited on
enter a number: 20

1! 
2! 
6! 
24! 
120! 
720! 
5040! 
40320! 
362880! 
3628800! 
39916800! 
479001600! 
1932053504! 
1278945280! 
2004310016! 
2004189184! 
-288522240! 
-898433024! 
109641728! 
-2102132736! 
the factorial number 20 is -2102132736


The maximum value for an int type variable is 2147483647. factorial *= counter; is making your value grow exponentially, hence why you reach that limit very quickly!
thanks for that explanaition, and interesting i did not identify the maximum value, i guess is by defefault on c++.
The maximum/minimum values can vary from system to system, but on most systems, a signed integer is 32-bit, and therefore is what hoogo said.

You can check your system by using numeric_limits.

1
2
3
4
5
6
7
8
9
10
11
12
13
// numeric_limits example
#include <iostream>     // std::cout
#include <limits>       // std::numeric_limits

int main () {
  std::cout << std::boolalpha;
  std::cout << "Minimum value for int: " << std::numeric_limits<int>::min() << '\n';
  std::cout << "Maximum value for int: " << std::numeric_limits<int>::max() << '\n';
  std::cout << "int is signed: " << std::numeric_limits<int>::is_signed << '\n';
  std::cout << "Non-sign bits in int: " << std::numeric_limits<int>::digits << '\n';
  std::cout << "int has infinity: " << std::numeric_limits<int>::has_infinity << '\n';
  return 0;
}


cpp.sh output:
Minimum value for int: -2147483648
Maximum value for int: 2147483647
int is signed: true
Non-sign bits in int: 31
int has infinity: false
Last edited on
The maximum value for an int type variable is 2147483647.
No it isn't. When using an int you actually don't know what it is - signed or unsigned, 16, 32, 64 bit or sth. totally different.

If you want to know what you are working with have a look at the types of cstdint
http://www.cplusplus.com/reference/cstdint/
Last edited on
Just change lines 15 and 16 to
1
2
	unsigned long long factorial = 1;
	unsigned long long user;

You will get to 20! (Just)

Line 27 doesn't actually make sense.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <limits>
#include <typeinfo>
#include <cmath>
using namespace std;


template <typename T> void maxFactorial()
{
   T maximum = numeric_limits<T>::max();
   int N = 1;
   while( tgamma( N + 1 ) <= maximum ) N++;
   N--;

   cout << "Type " << typeid( T ).name() << ";   maximum value: " << maximum << ";   maximum factorial: " << N << '\n';
}


int main()
{
   maxFactorial<short>();
   maxFactorial<unsigned>();
   maxFactorial<int>();
   maxFactorial<long>();
   maxFactorial<long long>();
   maxFactorial<unsigned long long>();
}


Type s;   maximum value: 32767;   maximum factorial: 7
Type j;   maximum value: 4294967295;   maximum factorial: 12
Type i;   maximum value: 2147483647;   maximum factorial: 12
Type l;   maximum value: 2147483647;   maximum factorial: 12
Type x;   maximum value: 9223372036854775807;   maximum factorial: 20
Type y;   maximum value: 18446744073709551615;   maximum factorial: 20


Makes me feel that factorial( N ) isn't all that useful a function.
Last edited on
thank you all, why line number 27th hasn't any sense? it´s supposed to show up the factorial nomber.

by the way, there are lots of new information about that simple problem guys. but i don't sure about limitations on differents systems. isn´t it supposed to all systems has a ordinary dafault characteristics?
Line 27 didn't make sense as output, that is all. The '!' is applied to the number whose factorial was being taken, not the result. e.g. 4! = 24, not 24!, which we've just ascertained that you couldn't compute (as an int).

No, different systems have different characteristics. The c++ standard often requires them to have minimum levels of accuracy, but doesn't set the maximum.
thanks master jedi
Topic archived. No new replies allowed.