We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,485 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

factorial in c++

#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?

7
Contributors
22
Replies
3 Days
Discussion Span
1 Year Ago
Last Updated
24
Views
seraphina
Newbie Poster
1 post since Nov 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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);
}
rubberman
Posting Maven
2,581 posts since Mar 2010
Reputation Points: 365
Solved Threads: 308
Skill Endorsements: 52
#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");
}
Dakot
Light Poster
35 posts since Nov 2011
Reputation Points: 24
Solved Threads: 1
Skill Endorsements: 0

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

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

WaltP
Posting Sage w/ dash of thyme
Team Colleague
11,404 posts since May 2006
Reputation Points: 3,421
Solved Threads: 1,055
Skill Endorsements: 37

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

selina12
Newbie Poster
13 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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;
}
Dakot
Light Poster
35 posts since Nov 2011
Reputation Points: 24
Solved Threads: 1
Skill Endorsements: 0

why use double for a factorial problem?

frogboy77
Posting Pro in Training
481 posts since Aug 2010
Reputation Points: 100
Solved Threads: 39
Skill Endorsements: 1

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);
}
frogboy77
Posting Pro in Training
481 posts since Aug 2010
Reputation Points: 100
Solved Threads: 39
Skill Endorsements: 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?

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

Dakot
Light Poster
35 posts since Nov 2011
Reputation Points: 24
Solved Threads: 1
Skill Endorsements: 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.

frogboy77
Posting Pro in Training
481 posts since Aug 2010
Reputation Points: 100
Solved Threads: 39
Skill Endorsements: 1

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?

Dakot
Light Poster
35 posts since Nov 2011
Reputation Points: 24
Solved Threads: 1
Skill Endorsements: 0

no

frogboy77
Posting Pro in Training
481 posts since Aug 2010
Reputation Points: 100
Solved Threads: 39
Skill Endorsements: 1

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 ).

Dakot
Light Poster
35 posts since Nov 2011
Reputation Points: 24
Solved Threads: 1
Skill Endorsements: 0

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

frogboy77
Posting Pro in Training
481 posts since Aug 2010
Reputation Points: 100
Solved Threads: 39
Skill Endorsements: 1

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 :]

Dakot
Light Poster
35 posts since Nov 2011
Reputation Points: 24
Solved Threads: 1
Skill Endorsements: 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.

WaltP
Posting Sage w/ dash of thyme
Team Colleague
11,404 posts since May 2006
Reputation Points: 3,421
Solved Threads: 1,055
Skill Endorsements: 37

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.. :[

Dakot
Light Poster
35 posts since Nov 2011
Reputation Points: 24
Solved Threads: 1
Skill Endorsements: 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... :-)

rubberman
Posting Maven
2,581 posts since Mar 2010
Reputation Points: 365
Solved Threads: 308
Skill Endorsements: 52

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.

rubberman
Posting Maven
2,581 posts since Mar 2010
Reputation Points: 365
Solved Threads: 308
Skill Endorsements: 52

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;
}
AbEeR Q8
Newbie Poster
10 posts since Oct 2011
Reputation Points: 11
Solved Threads: 0
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.1590 seconds using 2.73MB