#include<iostream>
using namespace std;
int main()
{
int num,factorial(int n) ;
cout<<"Enter a number "<<endl;
cin>>num;
}
for(n=0;n<100;n++)
{
cout<<""<<n<<endl;
system("pause");
}
where i do wrongly? why i cannot compile?
#include<iostream>
using namespace std;
int main()
{
int num,factorial(int n) ;
cout<<"Enter a number "<<endl;
cin>>num;
}
for(n=0;n<100;n++)
{
cout<<""<<n<<endl;
system("pause");
}
where i do wrongly? why i cannot compile?
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);
}
#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");
}
Second, there is no space between #include and <iostream>.
So? What's the problem you're pointing out?
#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;
}
}
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;
}
why use double for a factorial problem?
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);
}
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?
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.
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?
no
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 ).
No, you are using double for a larger range of values. In this instance it is no more precise.
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 :]
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.
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.. :[
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... :-)
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.
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;
}
Recursion can be a very elegant solution to a number of problems, but there are times when reverting to a looping construct instead may be preferable from the performance and memory (stack) usage perspective. It all depends upon how deep the recursion is going to get. This is how I would encode this one:
#include <iostream>
using namespace std;
unsigned long long factorial (unsigned long long x)
{
return (unsigned long long) (( x==0 ) ? 1 : x * factorial(x-1));
}
int main(void)
{
unsigned long long num = 0;
cout<<"Enter a number "<<endl;
cin>>num;
cout << dec << num << "! == " << dec << factorial(num) << endl;
// system("pause"); - don't use this - not portable.
return 0;
}
// system("pause"); - don't use this - not portable.
Is long long portable?
Is long long portable?
It is with current compilers. On some 64-bit systems, a long integer is 64-bits and regular is 32-bits, but long long will force it to use a 64-bit integer.