factorial program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dynamo
    New Member
    • Apr 2007
    • 51

    factorial program

    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:
    Code:
    #include<iostream.h>
    #include<string>
    using namespace std;
    int main(void){
        int fac;
        int n;
        string b;
        cout<<"this program produces the factorial of a number"<<"/n";
        cout<<"type an integer"<<"/n";
        cin>>n;
        if(n>0){
                int c=1;
                for(int a;n>=0;n=n-1){
                                     a=n-1;
                                     b=a*n;
                                     c=b*c;
                                     }
                cout<<c;}
        cin>>b;
    }
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

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

    Comment

    • dynamo
      New Member
      • Apr 2007
      • 51

      #3
      Originally posted by Ganon11
      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

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by dynamo
        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:
        Code:
        fac= 1;
        if (n < 1) stop;
        fac= fac*n;
        n= n-1;
        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:
        Code:
        fac= 1;
        i= 1;
        if (i >= n) stop;
        fac= fac*i;
        i= i+1;
        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

        Comment

        • pradeep kaltari
          Recognized Expert New Member
          • May 2007
          • 102

          #5
          Originally posted by dynamo
          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:
          Code:
          #include<iostream.h>
          #include<string>
          using namespace std;
          int main(void){
              int fac;
              int n;
              string b;
              cout<<"this program produces the factorial of a number"<<"/n";
              cout<<"type an integer"<<"/n";
              cin>>n;
              if(n>0){
                      int c=1;
                      for(int a;n>=0;n=n-1){
                                           a=n-1;
                                           b=a*n;
                                           c=b*c;
                                           }
                      cout<<c;}
              cin>>b;
          }
          Hi dynamo,
          Sorry I could not understand what you were trying to do inside the "for" loop.
          Thought the following would be helpful:
          Code:
          #include<iostream.h>
          
          int main()
          {
             int n,c=1;
             cout<<"Enter a positive integer:";
             cin>>n;
             if(n>0)
             {
                for(c=1;n>1;n--)
                {
                   c=c*n;
                }
             }
             cout<<"Factorial:"<<c;
          }
          You can also use recursive functions to do this job. The body of the function goes as follows:
          Code:
          int fact(int n)
          {
             return (n==0?1:n*fact(n-1));
          }
          (Assuming n is not negative)

          Hope this was helpful.

          Regards,
          Pradeep.

          Comment

          • Savage
            Recognized Expert Top Contributor
            • Feb 2007
            • 1759

            #6
            Originally posted by pradeep kaltari
            Hi dynamo,
            Sorry I could not understand what you were trying to do inside the "for" loop.
            Thought the following would be helpful:
            Code:
            #include<iostream.h>
            
            int main()
            {
               int n,c=1;
               cout<<"Enter a positive integer:";
               cin>>n;
               if(n>0)
               {
                  for(c=1;n>1;n--)
                  {
                     c=c*n;
                  }
               }
               cout<<"Factorial:"<<c;
            }
            You can also use recursive functions to do this job. The body of the function goes as follows:
            Code:
            int fact(int n)
            {
               return (n==0?1:n*fact(n-1));
            }
            (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

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Originally posted by Savage
              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

              Comment

              • Savage
                Recognized Expert Top Contributor
                • Feb 2007
                • 1759

                #8
                Originally posted by JosAH
                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

                Comment

                • dynamo
                  New Member
                  • Apr 2007
                  • 51

                  #9
                  Thanks for all the help.I have not been on the internet of recent or I would have been
                  active in the discussion.Than ks again,you have covered every aspect but i would like to ask what makes c++ so special.

                  Comment

                  • Savage
                    Recognized Expert Top Contributor
                    • Feb 2007
                    • 1759

                    #10
                    Originally posted by dynamo
                    Thanks for all the help.I have not been on the internet of recent or I would have been
                    active in the discussion.Than ks 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

                    Comment

                    • s04132
                      New Member
                      • May 2007
                      • 2

                      #11
                      #include<iostre am.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;
                      }

                      Comment

                      • dynamo
                        New Member
                        • Apr 2007
                        • 51

                        #12
                        Originally posted by Savage
                        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.

                        Comment

                        • Savage
                          Recognized Expert Top Contributor
                          • Feb 2007
                          • 1759

                          #13
                          Originally posted by dynamo
                          Interesting discussion.Does not explain why C++ is better than python though.
                          :P

                          Savage

                          Comment

                          Working...