473,404 Members | 2,179 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

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 4043
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
pradeep kaltari
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)

Hope this was helpful.

Regards,
Pradeep.
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)

Hope this was helpful.

Regards,
Pradeep.
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
s04132
2
#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

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Martin Jørgensen | last post by:
Hi, Consider (factorial.cpp): #include <iostream> using namespace std; double R=3.2; /* not used, but R is static because it is a global variable (file scope) */
35
by: aNt17017 | last post by:
This is my code: long fact(int n) { if (n == 0) return(1); if(n > 100) { printf("\t\tERROR: %d is too large for factorial.\n", n); return 1;
8
by: salman | last post by:
this program is giving compile time error. so plse ge me the logic of factorial # include <iostream.h> # include <math.h> void main() { int f,sum=0,i,j,n; cout<<"\nEnter Number: ";
1
by: Tin | last post by:
Dear Sir/Madam, I have one assignment , which need me to write a program to calculate the factorial of an integer, then press the button that would display the result in a label.3 & the...
10
by: yusuke | last post by:
can you please answer this:thanks!!! write a factorial function that returns a factorial of a long int number. Enter the number at the main funtion and call factorial() to carry on calculations...
59
by: Umesh | last post by:
i wrote the following program to calculate factorial: #include<stdio.h> #include<iostream.h> void main() { int i,n; long int p=1; // or long double p=1; for exponential result which I don't...
3
by: Sugandh Jain | last post by:
Hi. How to write a function that will return me the factorial (say in a string) for the any positive integer it takes? When we find a factorial of even say 2000 or a higher number, it will be...
2
by: becky808 | last post by:
Hi, I'm trying to write a program to calculate n factorial but it won't compile. Can anyone tell me what I'm doing wrong? #include <iostream> #include <cmath> using namespace std; int...
3
by: Blue sky | last post by:
Hi,I am a new C++ learner.The follow prgram produces a wrong result,but I can't find the wrong.Can you help me?Thank you! #include<stdio.h> long factorial( long number); int main() { int...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.