Hello,
I've just wrote a small program to calculate the factorial of a given number. When i give a small number everything is ok but when i want t calculate the factorial of a biger number, like 53!, I get a wrong answer.
I thing that the problec is the type of the type I use(unsigned long int).
Can anyone please helpmewith that;

unfortunately there is really nothing you can do about it except to use a 64-bit integer, such as long long or _int64. What you are experiencing is data overflow -- integers can only hold a finite number of digits. The file limits.h contains macros that are the maximum values for various data types.

if you are interested in only an approximate value, you could use a type like double for computing the result. for example, a long double can hold very large values, but precision is limited (on my compiler, the mantissa is accurate upto 15 decimal digits).

#include <limits>
#include <iostream>
#include <iomanip>
using namespace std;

inline long double factorial( unsigned int n )
{
   long double result = 1.0 ;
   while( n>1 ) result *= n-- ;
   return result ;
}

int main()
{
  cout << "unsigned int-\t" << numeric_limits< unsigned int >::max() << '\t'
       << numeric_limits< unsigned int >::digits10 << " decimal digits\n" ;
  cout << "long double-\t" << numeric_limits< long double >::max() << '\t'
       << numeric_limits< long double >::digits10 << " decimal digits\n" ; ;

  cout << fixed << setprecision(0) << "53! ~= " << factorial(53) << '\n' ;
}
/**
>g++ -Wall -std=c++98 num_limits.cpp && ./a.out
unsigned int-   4294967295      9 decimal digits
long double-    1.18973e+4932   15 decimal digits
53! ~= 4274883284060023952295713899453537359909467862005827442747675726839808
*/

for accurate results, you could use a library eg. https://sourceforge.net/projects/cpp-bigint/

commented: Nicely Presented +3

thnx everyone for the answers. I search google for my problem and i found that their are some librariesfor big numbers that some one can download and use but unfortunatly I cannot use them for my homework.
If I want just to store a number of 200 characters there is no type for it ?????????

> if I want just to store a number of 200 characters there is no type for it ?????????
there is no *builtin* type for it. you can create a *userdefined* type which can be used like an int and has 200 decimal digits of precision.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.