factorial program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • yusuke
    New Member
    • Mar 2007
    • 7

    factorial program

    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 and return the result.
  • chella
    New Member
    • Mar 2007
    • 51

    #2
    Originally posted by yusuke
    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 and return the result.

    Hi,
    U try writing the code and let us know if u have any problem with it.

    Regards,
    Chella

    Comment

    • yusuke
      New Member
      • Mar 2007
      • 7

      #3
      sorry,, i really dont know... im still a new student of programming and this program is for advance study. can u help me w/ this?
      thanks!

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        The experts on this site are more than happy to help you with your problems but they cannot do your assignment/program for you. Attempt the assignment/program yourself first and post questions regarding any difficulties you have or about a particular function of the code that you don't know how to achieve.

        Please read the Posting Guidelines and particularly the Coursework Posting Guidlines.

        Then when you are ready post a new question in this thread.

        Administrator

        Do you know how to calculate a factorial on paper? Say 5!.

        Comment

        • Mack
          New Member
          • Feb 2007
          • 11

          #5
          Try using Recursion else simple for loop will work for you , I understand you are beginner same was with me , although i respect all alongwith you.
          something like this will go .
          see
          for (i=0; i<num;i++)
          i=i*(i-1)

          retun(i)

          see this is not way its going to work its just clue to you , so u get enthu to work further on it.

          Regards,
          Mack.

          Comment

          • Extremist
            New Member
            • Jan 2007
            • 94

            #6
            Here's a tip:

            Think recursion, it makes your function about 5 lines...

            Not so hard...

            Go have a look in the forum, There is a complete Factorial recursion function here...
            ;-)

            Comment

            • yusuke
              New Member
              • Mar 2007
              • 7

              #7
              Originally posted by Banfa
              The experts on this site are more than happy to help you with your problems but they cannot do your assignment/program for you. Attempt the assignment/program yourself first and post questions regarding any difficulties you have or about a particular function of the code that you don't know how to achieve.

              Please read the Posting Guidelines and particularly the Coursework Posting Guidlines.

              Then when you are ready post a new question in this thread.

              Administrator

              Do you know how to calculate a factorial on paper? Say 5!.
              yes i can... the factorial of 5 is 120...thanks!!!

              Comment

              • infonite
                New Member
                • Mar 2007
                • 3

                #8
                Originally posted by yusuke
                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 and return the result.
                <code snipped in accordance with the posting guide lines>

                <edit=Banfa>
                Please read the Posting Guidelines and particularly the Coursework Posting Guidlines.</edit>
                Last edited by Banfa; Mar 10 '07, 09:22 AM. Reason: Removed code in accordance with posting guidelines

                Comment

                • Banfa
                  Recognized Expert Expert
                  • Feb 2006
                  • 9067

                  #9
                  Originally posted by yusuke
                  yes i can... the factorial of 5 is 120...thanks!!!
                  So you know the mathematical method to get the result of a factorial.

                  The next step is to write that method down as a series of simple instructions (in any language you like)

                  And finally you convert those instructions into C

                  Comment

                  • yusuke
                    New Member
                    • Mar 2007
                    • 7

                    #10
                    Originally posted by Banfa
                    So you know the mathematical method to get the result of a factorial.

                    The next step is to write that method down as a series of simple instructions (in any language you like)

                    And finally you convert those instructions into C
                    Code:
                    #include<stdio.h>
                    int c(int a);
                    main()
                    {
                    	int a;
                    	printf("Enter a number:");
                    	scanf("%d",&a);
                    
                    	printf("The factorial number is: ");
                    	printf("%d",c(a));
                    	return 0;
                    }
                    int c(int a)
                    {
                    	int b,z;
                    	b=1;
                    	for(z=1;z<=a;z++){
                    	b=b*z;
                    	}
                    	return b;
                    }
                    i think i got it right this time...thank u guyzzzzzzzz!!!! !!
                    Last edited by Ganon11; Mar 12 '07, 12:05 PM. Reason: code tags added

                    Comment

                    • Banfa
                      Recognized Expert Expert
                      • Feb 2006
                      • 9067

                      #11
                      Looks like it will work, however

                      1. c is not a good name for a function

                      2. There is a limit to the input number after which overflow of the integer will cause the answer to be wrong. You may want to consider finding the limit and putting in a check to reject numbers that are too big to calculate.

                      Comment

                      Working...