1,105,333 Community Members

factorial in c++

Member Avatar
seraphina
Newbie Poster
1 post since Nov 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 
#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?

Member Avatar
rubberman
Senior Poster
3,986 posts since Mar 2010
Reputation Points: 513 [?]
Q&As Helped to Solve: 500 [?]
Skill Endorsements: 87 [?]
 
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);
}
Member Avatar
Dakot
Light Poster
35 posts since Nov 2011
Reputation Points: 14 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
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");
}
Member Avatar
WaltP
Posting Sage w/ dash of thyme
9,363 posts since May 2006
Reputation Points: 2,905 [?]
Q&As Helped to Solve: 1,151 [?]
Skill Endorsements: 45 [?]
Team Colleague
 
0
 

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

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

Member Avatar
selina12
Newbie Poster
13 posts since Nov 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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;
}
}

Member Avatar
Dakot
Light Poster
35 posts since Nov 2011
Reputation Points: 14 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
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;
}
Member Avatar
frogboy77
Posting Pro in Training
477 posts since Aug 2010
Reputation Points: 73 [?]
Q&As Helped to Solve: 43 [?]
Skill Endorsements: 1 [?]
 
0
 

why use double for a factorial problem?

Member Avatar
frogboy77
Posting Pro in Training
477 posts since Aug 2010
Reputation Points: 73 [?]
Q&As Helped to Solve: 43 [?]
Skill Endorsements: 1 [?]
 
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);
}
Member Avatar
Dakot
Light Poster
35 posts since Nov 2011
Reputation Points: 14 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
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?

p.s http://www.cplusplus.com/doc/tutorial/variables/

Member Avatar
frogboy77
Posting Pro in Training
477 posts since Aug 2010
Reputation Points: 73 [?]
Q&As Helped to Solve: 43 [?]
Skill Endorsements: 1 [?]
 
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.

Member Avatar
Dakot
Light Poster
35 posts since Nov 2011
Reputation Points: 14 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
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?

Member Avatar
frogboy77
Posting Pro in Training
477 posts since Aug 2010
Reputation Points: 73 [?]
Q&As Helped to Solve: 43 [?]
Skill Endorsements: 1 [?]
 
0
 

no

Member Avatar
Dakot
Light Poster
35 posts since Nov 2011
Reputation Points: 14 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
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 ).

Member Avatar
frogboy77
Posting Pro in Training
477 posts since Aug 2010
Reputation Points: 73 [?]
Q&As Helped to Solve: 43 [?]
Skill Endorsements: 1 [?]
 
0
 

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

Member Avatar
Dakot
Light Poster
35 posts since Nov 2011
Reputation Points: 14 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
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 :]

Member Avatar
WaltP
Posting Sage w/ dash of thyme
9,363 posts since May 2006
Reputation Points: 2,905 [?]
Q&As Helped to Solve: 1,151 [?]
Skill Endorsements: 45 [?]
Team Colleague
 
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.

Member Avatar
Dakot
Light Poster
35 posts since Nov 2011
Reputation Points: 14 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
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.. :[

Member Avatar
rubberman
Senior Poster
3,986 posts since Mar 2010
Reputation Points: 513 [?]
Q&As Helped to Solve: 500 [?]
Skill Endorsements: 87 [?]
 
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... :-)

Member Avatar
rubberman
Senior Poster
3,986 posts since Mar 2010
Reputation Points: 513 [?]
Q&As Helped to Solve: 500 [?]
Skill Endorsements: 87 [?]
 
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.

Member Avatar
AbEeR Q8
Newbie Poster
10 posts since Oct 2011
Reputation Points: 1 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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;
}
You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article