468,306 Members | 1,132 Online

# factorial program

51
Hello everyone,i am a beginner in c++ and i am trying to write a program that outputs the factorial of a number in my own way,however i don't know why it's not working.Thanks for any help.This is the code:
Expand|Select|Wrap|Line Numbers
1. #include<iostream.h>
2. #include<string>
3. using namespace std;
4. int main(void){
5.     int fac;
6.     int n;
7.     string b;
8.     cout<<"this program produces the factorial of a number"<<"/n";
9.     cout<<"type an integer"<<"/n";
10.     cin>>n;
11.     if(n>0){
12.             int c=1;
13.             for(int a;n>=0;n=n-1){
14.                                  a=n-1;
15.                                  b=a*n;
16.                                  c=b*c;
17.                                  }
18.             cout<<c;}
19.     cin>>b;
20. }
May 23 '07 #1
12 3678
Ganon11
3,652 Expert 2GB
There are a couple things wrong with your program, as well as with your algorithm:

First, you have declared an integer named fac in the first line of main() - when do you use it? Were you planning to use it?

Second, you declared b as a string, but then try to use it like an integer within your loop - why is it a string? Should it be an int?

I'm not sure how your loop is supposed to be finding the factorial of n - could you try and explain what you're trying to do?
May 23 '07 #2
dynamo
51
There are a couple things wrong with your program, as well as with your algorithm:

First, you have declared an integer named fac in the first line of main() - when do you use it? Were you planning to use it?

Second, you declared b as a string, but then try to use it like an integer within your loop - why is it a string? Should it be an int?

I'm not sure how your loop is supposed to be finding the factorial of n - could you try and explain what you're trying to do?
Thanks for taking the time.Firstly c was supposed to be fac.So try to ignore fac.[mistakes I made after the frustration of not running the program successfully]b is supposed to be an integer and there is supposed to be another variable at”cin>>b” which should make the program wait for user input”I am trying to decrement n continuously while it it is greater than zero.
Since fac of n=n*n-1*n-2
So what I am trying to do is to reduce n continuously and multiply it by a[which is n-1]
And I try to store the value of n*n-1 in b then store b*c in c.Where c at first is one,but afterwards will take the previous value of c and multiply it by the new value of b which is updated for each iteration due to the fact that n is being decreased. Therefore when the loop stops [when n ceases to be greater than 0]the value of c should be the factorial of n
May 23 '07 #3
JosAH
11,448 Expert 8TB
Since fac of n=n*n-1*n-2
So what I am trying to do is to reduce n continuously and multiply it by a[which is n-1]
And I try to store the value of n*n-1 in b then store b*c in c.Where c at first is one,but afterwards will take the previous value of c and multiply it by the new value of b which is updated for each iteration due to the fact that n is being decreased. Therefore when the loop stops [when n ceases to be greater than 0]the value of c should be the factorial of n
Now you've made me dizzy ;-) This is how I would do it by hand. 'n' is the
number for which the factorial should be calculated:
Expand|Select|Wrap|Line Numbers
1. fac= 1;
2. if (n < 1) stop;
3. fac= fac*n;
4. n= n-1;
5. goto step 2;
This does all the multiplications starting at the highest numbers; if you want to
start at the lowest numbers something like this will do:
Expand|Select|Wrap|Line Numbers
1. fac= 1;
2. i= 1;
3. if (i >= n) stop;
4. fac= fac*i;
5. i= i+1;
6. goto step 3;
All you have to do now is translate that pseudo-code to C/C++ and remove the
unused variables.

kind regards,

Jos
May 23 '07 #4
102 Expert 100+
Hello everyone,i am a beginner in c++ and i am trying to write a program that outputs the factorial of a number in my own way,however i don't know why it's not working.Thanks for any help.This is the code:
Expand|Select|Wrap|Line Numbers
1. #include<iostream.h>
2. #include<string>
3. using namespace std;
4. int main(void){
5.     int fac;
6.     int n;
7.     string b;
8.     cout<<"this program produces the factorial of a number"<<"/n";
9.     cout<<"type an integer"<<"/n";
10.     cin>>n;
11.     if(n>0){
12.             int c=1;
13.             for(int a;n>=0;n=n-1){
14.                                  a=n-1;
15.                                  b=a*n;
16.                                  c=b*c;
17.                                  }
18.             cout<<c;}
19.     cin>>b;
20. }
Hi dynamo,
Sorry I could not understand what you were trying to do inside the "for" loop.
Thought the following would be helpful:
Expand|Select|Wrap|Line Numbers
1. #include<iostream.h>
2.
3. int main()
4. {
5.    int n,c=1;
6.    cout<<"Enter a positive integer:";
7.    cin>>n;
8.    if(n>0)
9.    {
10.       for(c=1;n>1;n--)
11.       {
12.          c=c*n;
13.       }
14.    }
15.    cout<<"Factorial:"<<c;
16. }
17.
You can also use recursive functions to do this job. The body of the function goes as follows:
Expand|Select|Wrap|Line Numbers
1. int fact(int n)
2. {
3.    return (n==0?1:n*fact(n-1));
4. }
5.
(Assuming n is not negative)

Regards,
May 23 '07 #5
Savage
1,764 Expert 1GB
Hi dynamo,
Sorry I could not understand what you were trying to do inside the "for" loop.
Thought the following would be helpful:
Expand|Select|Wrap|Line Numbers
1. #include<iostream.h>
2.
3. int main()
4. {
5.    int n,c=1;
6.    cout<<"Enter a positive integer:";
7.    cin>>n;
8.    if(n>0)
9.    {
10.       for(c=1;n>1;n--)
11.       {
12.          c=c*n;
13.       }
14.    }
15.    cout<<"Factorial:"<<c;
16. }
17.
You can also use recursive functions to do this job. The body of the function goes as follows:
Expand|Select|Wrap|Line Numbers
1. int fact(int n)
2. {
3.    return (n==0?1:n*fact(n-1));
4. }
5.
(Assuming n is not negative)

Regards,
This is the best way.

However I would rather make n to be of
unsigned long int type, because for a little bigger number n u allready pass int limit of 32,767, and function to return unsigned long int too!

:)

Savage
May 23 '07 #6
JosAH
11,448 Expert 8TB
This is the best way.

However I would rather make n to be of
unsigned long int type, because for a little bigger number n u allready pass int limit of 32,767, and function to return unsigned long int too!

:)

Savage
According to the C standard sizeof(int) <= sizeof(long) and on a lot of nowaday
systems both ints and longs are at least four bytes in size. The unsigned
qualifier doesn't buy you much because the unsigned version max number is
just twice the signed number max number, e.g. for both signed and unsigned
four byte values the maximum factorial number they can hold is 13! and 13 > 2.

kind regards,

Jos
May 23 '07 #7
Savage
1,764 Expert 1GB
According to the C standard sizeof(int) <= sizeof(long) and on a lot of nowaday
systems both ints and longs are at least four bytes in size. The unsigned
qualifier doesn't buy you much because the unsigned version max number is
just twice the signed number max number, e.g. for both signed and unsigned
four byte values the maximum factorial number they can hold is 13! and 13 > 2.

kind regards,

Jos
Better then short int which can hold up to 8!

I guess that long double is than a best solution.

It can go up to 1755!

Savage
May 23 '07 #8
dynamo
51
Thanks for all the help.I have not been on the internet of recent or I would have been
active in the discussion.Thanks again,you have covered every aspect but i would like to ask what makes c++ so special.
May 26 '07 #9
Savage
1,764 Expert 1GB
Thanks for all the help.I have not been on the internet of recent or I would have been
active in the discussion.Thanks again,you have covered every aspect but i would like to ask what makes c++ so special.
We are more then happy to help u!

What makes C++ special?

Take a look here

Savage
May 26 '07 #10
#include<iostream.h>
int main(void)
{

int number;
int sum=1;
cout<<"Enter number for factorial :";
cin>>number;
for(int i=number;i>=1;i--)
{
sum*=number;
number--;
}
cout<<sum;
}
May 27 '07 #11
dynamo
51
We are more then happy to help u!

What makes C++ special?

Take a look here

Savage
Interesting discussion.Does not explain why C++ is better than python though.
May 29 '07 #12
Savage
1,764 Expert 1GB
Interesting discussion.Does not explain why C++ is better than python though.
:P

Savage
May 29 '07 #13