Start New Discussion within our **Software Development Community** Not Yet Answered # factorial in c++

rubberman 971 Dakot 14 WaltP 2,905 selina12 Dakot 14 frogboy77 73 Dakot 14 frogboy77 73 Dakot 14 Dakot 14 frogboy77 73 Dakot 14 WaltP 2,905 Dakot 14 rubberman 971 Featured Reply rubberman 971 AbEeR Q8 1

0

Well, first of all, this is not a factorial computation. Second, there is no space between #include and <iostream>. Third, you don't indicate what errors you are getting, although you are declaring (incorrectly) `factorial(int n)`

in your local variable declaration list. It is (probably), an external function, and should be declared before main(), as in:

```
#include <iostream>
using namespace std;
extern int factorial(int); // Unnecessary in the context of this code.
int main(void)
{
int num, n;
cout<<"Enter a number "<<endl;
cin>>num;
for(n=num; num>1 ;num--)
{
n *= (num-1);
cout << n << endl;
system("pause"); // Is this necessary?
}
printf("Factorial(%d) == %d\n", num, n);
}
```

0

```
#include<iostream>
using namespace std;
int main()
{
int num,factorial(int n) ;
cout<<"Enter a number "<<endl;
cin>>num;
} //your main function ends here , but it must end at the end of the code <<<<<--
for(n=0;n<100;n++)
{
cout<<""<<n<<endl;
system("pause");
}
```

0

Second, there is no space between #include and <iostream>.

So? What's the problem you're pointing out?

0

#include<iostream.h>

#include<conio.h>

void main()

{

int n, fact;

int rec(int);

cout<<"Enter the Number";

cin>>n;

fact=rec(n);

cout<<"Factorial is : "<<fact<<endl;

getch();

}

int rec(int x)

{

int f;

if(x==1)

{

return x;

}

else

{

f=x*rec(x-1);

return f;

}

}

0

here it is

```
#include <cstdlib>
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int fNumber;
double fact=1.0;
cout<<"Enter a number to calculate it's factorial: "<<endl;
cin>>fNumber;
for (int i = 1 ; i<=fNumber; i++) {
fact *= i;
}
cout<<"The Factorial is :"<<fact<<endl;
getch();
return 0;
}
```

0

what about

```
#include <iostream>
using namespace std;
int fact(int a);
int main()
{
int num;
cout<<"Input number.\n";
cin>>num;
cout<<fact(num)<<endl;
return 0;
}
int fact(int a)
{
if(a==1)return 1;
else return a*fact(a-1);
}
```

0

ok , let me explain. First run&compile my code , when ask to input a number, type 50 , see the output? . Now change this line

double fact=1.0;

to

int fact=1;

, run&compile , see the differences?

0

ok run your code and input 100.

Unless you are using vectors or strings to do this there has to be a defined limit.

c++ not python or ruby.

0

Unless you are using vectors or strings to do this there has to be a defined limit.

sure)) , but in this case , double is better than int ;) are you agree with that?

0

no

ok)

p.s i use double for more precision ,and in my opinion using it in such cases is much better than using int ).

0

No, you are using double for a larger range of values. In this instance it is no more precise.

0

No, you are using double for a larger range of values. In this instance it is no more precise.

who cares))ok it is no more precise , and i use double for a larger range of values , is it wrong to use double instead of int in such cases? i think no :]

0

Ahhh, we see. You are not a serious student of programming. You are just a hobbyist and don't really need to learn and understand the proper terminology. You only want to program for fun. That's fine. Just keep in mind many of us are professionals, and most are striving to *be* professionals, so if we correct you, it's not you. It's just our need to be accurate.

1

hey man , maybe i'm not a serious student, but i'm trying to be). And no , i'm not a hobbyist , i want to learn seriosly programming languages (c++ , java , javascript , php etc.) I don't want to program for fun , but to accumulate knowledge. Ok , i agree with you , i behaved a little stupid , i'm sorry . But can you tell me what are the differences between int and double in such a case? which one is the best to use in such a program and why?

p.s sorry.. :[

0

For something like factorial operations that typically operate on integer values, whether you want to use double precision floating point values depends a lot on what the largest result value is likely to be. If you use 64-bit integers (long long int), then this is not such a problem, but before compilers supported 64-bit integers, where even a long integer would be a 32-bit values, this was often necessary, as a 32-bit signed integer can only represent a range of +-2147483647, or 4,294,967,295 if you are using unsigned integers. So, the factorial of a fairly large number can easily overflow a 32-bit value. An unsigned 64-bit unsigned integer can be up to 18446744073709551615 - or 19+ digits, which is, for integer values, preferable to double precision (64-bit) numbers which are typically limited to about 15 digits of accuracy, and they suffer from intermediate rounding errors that integers will not.

Note that 20! == 2432902008176640000 will almost overflow a 64-bit integer, and cannot be properly represented with a double. New compilers will support long doubles (128 bits), so the march to ever bigger numbers continues... :-)

1

Also, if you are interested, 50! == 30414093201713378043612608166064768844377641568960512000000000000

and 100! == 9.332621544e+157, or 9,332,621,544 with 148 extra zeros tacked onto the end.

0

for the question here , this is what I program by using recursive function and with out any loop in the main

I hope my program helps you

```
#include <iostream>
using namespace std;
int factorial (int x){
if ( x==0 )
return 1;
else
return x * factorial(x-1);
}
int main(){
int num ;
cout<<"Enter a number "<<endl;
cin>>num;
cout<<factorial (num)<<endl;
system("pause");
return 0;
}
```

This article has been dead for over six months. Start a new discussion instead.