Thread: Factorial

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    18

    Factorial

    I'm just starting in C, and I'm trying to use the factorial function and print the result from entering a nonnegative integer. Anybody got some ideas. By the way the error message says: Lvalue required. Thanks DW

    #include<stdio.h>

    long factorial(long number); /* function prototype */

    void main(void)

    {

    int x; /* Integer number to be input by user*/

    printf("Enter integer\n"); /* Prompt */

    scanf("%d",&x); /* Prompt */

    factorial=1;

    printf("%d\n", factorial);

    }

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Hi don first off please use code tags. Second your calling a function that you havn't declared just prototyped. let me show you how it would be done.
    Code:
    #include<stdio.h>
    
    long factorial(long number); /* function prototype */
    
    int main(void)/*void main is wrong don't use it unless you know you need to use it!*/
    {
      int x; /* Integer number to be input by user*/
      printf("Enter integer\n"); /* Prompt */
      scanf("%d",&x); /* Prompt */
      int getFactorial=factorial(x);/*Setting a variable to the return value of factorial*/
      printf("%d\n", getFactorial);
    }
    
    long factorial(long number)
    {
      /*factorial code I'll leave for you to do :)*/
      return number;
    }
    Last edited by prog-bman; 09-18-2004 at 03:34 AM.
    Woop?

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    Declarations usually mean prototypes. The error is the result of the line "factorial = 1." The identifier "factorial" may only be used as an r-value, which is an expression on the right side of an assignment, in constrast with an l-value, which is the expression on the left side of an assignment.
    Last edited by okinrus; 09-18-2004 at 03:46 AM.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    18
    I have changed the code a small amount, it compiles, and runs, it requests an integer, but prints out only a 0......Hmmmmm


    #include<stdio.h>

    int factorial;

    void main(void)

    {

    int x; /* Integer number to be input by user*/

    printf("Enter integer\n"); /* Prompt */

    scanf("%d",&x); /* Prompt */


    printf("\nThe factorial is %d", factorial);

    }

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    You're not ever setting your factorial variable to anything...where's your algorithm for finding the factorial? Calling a variable "factorial" isn't a hint to the C compiler that it should generate factorials for you.
    If you understand what you're doing, you're not learning anything.

  6. #6
    Watch for flying houses. Nessarose's Avatar
    Join Date
    Sep 2004
    Posts
    46
    Perhaps this would make more sense if you had a similar example:

    Code:
    #include <stdio.h>
    
    /* the sum function calculates the sum of two integers x and y */
    int sum(int x, int y);
    
    int main(void) {
         int x = 10;
         int y = 5;
         int answer = sum(x, y);
         printf("Answer: %d\n", answer);
    
         return 0;
    }
    
    int sum(int x, int y) {
        return x + y;
    }
    Instead of factorial, I'm calculating the sum of two integers. As you can see, it's not enough to just call and declare the function sum, you must have the code written out that will calculate the sum of the two integers and return it's value back to the function that called it (main()). So in your case, you need to write the code for the factorial function.

  7. #7
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    With this line of code

    int factorial;

    you declare a variable of type int. This variable can contain some value. In your program, this line of code

    printf("\nThe factorial is %d", factorial);

    does nothing but printing the actual value of variable factorial. To really calculate the factorial of some number, you must implement the function which prototype was

    long factorial (long number);

    and call that function in your main() function. Here factorial is the name of the function and number is the argument passed to the function. The long before number tells that this variable is of type long and the long before factorial tells that the function returns a value of type long.

    Note that C does not have a factorial function itself, you will need to implement it by yourself.

    Regards,
    Shiro

  8. #8
    Registered User
    Join Date
    Sep 2004
    Posts
    18

    Thanks I went a little different way

    My new code. it works maybe not the best, but it works. Het all of you whom commented, much kudos and love, great ideas and made me get down and dirty....


    #include <stdio.h>

    double fact(int n);

    int main(void)

    {
    int current;

    printf("Enter a positive integer [to terminate enter non-positive] > ");
    scanf("%d", &current);
    while (current >= 0)

    {
    printf("The factorial of %d is %.f\n", current, fact(current));
    printf("Enter a positive integer [to terminate enter non-positive] > ");
    scanf("%d", &current);
    }

    }

    /* n is a positive integer. The function returns its factorial */

    double fact(int n)

    {
    int lcv; /* loop control variable */
    double p; /* set to the product of the first lcv positive integers */

    for(p=1, lcv=2; lcv <= n; p=p*lcv, lcv++);
    return p;

    }

  9. #9
    Watch for flying houses. Nessarose's Avatar
    Join Date
    Sep 2004
    Posts
    46
    Good job. You could also have written it as a recursive function:

    Code:
    int fact(int n)
    {
        if (n <= 1)
            return 1;
        return n * fact(n - 1);
    }

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >You could also have written it as a recursive function
    Don't forget to add the requisite warning that this is a bad use of recursion because it doesn't simplify the problem enough to outweigh the disadvantages of recursion.
    My best code is written with the delete key.

  11. #11
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    If you plan to use factorials for other than educational purposes, use lookup tables.

    The factorial functions grows very fast, so the lookup table will be very small.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault using recursion to find factorial
    By kapok in forum C++ Programming
    Replies: 4
    Last Post: 02-23-2009, 11:10 AM
  2. Factorial
    By foxman in forum Contests Board
    Replies: 27
    Last Post: 07-11-2008, 06:59 PM
  3. Recursion
    By Lionmane in forum C Programming
    Replies: 11
    Last Post: 06-04-2005, 12:00 AM
  4. Basic Factorial from 1-10
    By AaA in forum C Programming
    Replies: 20
    Last Post: 05-28-2005, 07:39 AM
  5. factorial output
    By maloy in forum C Programming
    Replies: 1
    Last Post: 03-13-2002, 03:28 PM