+ 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; }

21st Jun 2018, 7:34 AM
Shehroz Irfan
Shehroz Irfan - avatar
14 Answers
+ 18
i'm sorry for late... but it seems it was answered..
21st Jun 2018, 9:04 AM
ZΛRTHΛИ
ZΛRTHΛИ - avatar
+ 9
AK-47 thanks
21st Jun 2018, 8:05 AM
Shehroz Irfan
Shehroz Irfan - avatar
+ 9
20th Jul 2018, 7:57 AM
Shehroz Irfan
Shehroz Irfan - avatar
21st Jun 2018, 8:03 AM
Shehroz Irfan
Shehroz Irfan - avatar
+ 6
21st Jun 2018, 7:35 AM
Shehroz Irfan
Shehroz Irfan - avatar
21st Jun 2018, 7:35 AM
Shehroz Irfan
Shehroz Irfan - avatar
+ 6
AK-47 Ok thanks
21st Jun 2018, 3:12 PM
Shehroz Irfan
Shehroz Irfan - avatar
21st Jun 2018, 7:34 AM
Shehroz Irfan
Shehroz Irfan - avatar
+ 5
21st Jun 2018, 7:35 AM
Shehroz Irfan
Shehroz Irfan - avatar
+ 5
ok
21st Jun 2018, 1:44 PM
Shehroz Irfan
Shehroz Irfan - avatar
+ 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) { //... }
21st Jun 2018, 8:04 AM
To Seek Glory in Battle is Glorious
To Seek Glory in Battle is Glorious - avatar
+ 3
because factorials grows so fastly and another suggestion if you wanna take it ,applying recursion method would be so cool at your code
19th Jul 2018, 8:13 PM
Emre İRİŞ
Emre İRİŞ - avatar
+ 2
shehroz irfan don't @ me please i'll come across this and answer it eventually provided there isn't an answer
21st Jun 2018, 12:23 PM
hinanawi
hinanawi - avatar
+ 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.
21st Jun 2018, 2:48 PM
To Seek Glory in Battle is Glorious
To Seek Glory in Battle is Glorious - avatar