+ 8
Program to find factorial.It didn't gives the factorial of Higher numbers like 125 .Can you fix it ? Or whats the problem in it?
#include <iostream> using namespace std; int main() { int number,factorial; factorial =1; cout<<"Enter the number of which you want to get the factorial\n"; cin>>number; while(number>=1) { factorial=factorial*number; number=number-1; } cout<<"The factorial of the number is:"<<factorial; return 0; }
14 Answers
+ 18
i'm sorry for late... but it seems it was answered..
+ 9
AK-47 thanks
+ 9
Emre İRİŞ thanks
+ 7
+ 6
\/.oz ?
+ 6
+ 6
AK-47
Ok thanks
+ 5
hinanawi ?
+ 5
Donna ?
+ 5
ok
+ 4
Factorial is an expensive function in terms of growth rate.
A simple query of integer size tells you that the maximum possible factorial which can be stored in that is 12! = 479001600
Since INT_MAX is 2147483647 and 13! would be way greater than this bound.
So, the way you can combat this issue is to inform the program that the upper bound is limited by the maximum factorial size (However, using unsigned long long type will increase the acceptable range to 20!)
typedef unsigned long long ull;
//...
ull factorial = 0;
//...
while (number >= 1 && number <= 20) {
//...
}
+ 3
because factorials grows so fastly and another suggestion if you wanna take it ,applying recursion method would be so cool at your code
+ 2
shehroz irfan don't @ me please i'll come across this and answer it eventually provided there isn't an answer
+ 2
shehroz irfan
In the case that the precision of the result isn't important, declaring factorial variable as double or long double would allows you to reach to 170! ~7.25741e+306 and 1754! ~1.97926e+4930, respectively considering the DBL_MAX's ~1.79769e+308 and LDBL_MAX's ~1.18973e+4932.