Search code examples
cstringalgorithmfactorial

Factorial Algorithm in C: way to store result of previous iteration in a string?


I'm just starting out with programming in C and I only know how to make some very simple programs in other languages. Anyway, I'm making an iterative factorial calculator (current progress can be seen below) and I'm not sure how I'd go about storing "factorial_value" to be used in the next iteration of the loop. If I run the code as is, then "factorial_value" will just reset itself to whatever user_number * (user_number - step) is equal to for that iteration rather than keeping the value form the previous iteration and multiplying that number by the number it succeeds.

Is there some way where I can store the result of each iteration by appending it to a string and then extracting the appropriate value in each following step?

#include <stdio.h>

int main()
{
    int user_number;
    int factorial_value = user_number;
    int step = 1;


    printf("Enter the nubmer you would like to get the factorial of: ");
    scanf("%d!",&user_number);

    while (step < user_number) {
        factorial_value = factorial_value * (user_number - step);

        step++;
    }

    printf("\n%d! = %d\n",user_number,factorial_value);

    return 0;
}

Solution

  • Your program is correct, except that you are using the value of the variable factorial_number before it is initialized. You could store the number in a string and read it again, but it would be slow, more complex, and doesn't seem necessary, given the code you have shown us.

    One way to fix the unitialized value is to assign user_number to it after it was read by scanf:

    #include <stdio.h>
    
    int main()
    {
        int user_number;
        int factorial_value;  /* just declare it here */
        int step = 1;
    
    
        printf("Enter the nubmer you would like to get the factorial of: ");
        scanf("%d!",&user_number);
    
        /* and now that user_number was rad, you can assign it to factorial_number: */
        factorial_value = user_number; 
    
        while (step < user_number) {
            factorial_value = factorial_value * (user_number - step);
    
            step++;
        }
    
        printf("\n%d! = %d\n",user_number,factorial_value);
    
        return 0;
    }
    

    But you may make your program a bit simpler if you multiply factorial_number by step in each iteration (see that I changed the condition from < to <= so the last number will be used). I also changed the types to unsigned long, so you can calculate factorials of larger numbers (int is a short integer. long is longer, and long long may be even longer in bits; the unsigned modifier means you will not represent negative numbers, so all bits will be used to represent magnitude -- the range is 2 times larger than its signed counterpart).

    #include <stdio.h>
    
    int main()
    {
        int user_number;
        unsigned long long factorial_value = 1; /* starts with 1 */
        int step = 1;
    
        printf("Enter the nubmer you would like to get the factorial of: ");
        scanf("%d!",&user_number);
    
        /* from one to user_number, including it: */
        while (step <= user_number) {
            /* multiply factorial_number by the current value of step: */
            factorial_value = factorial_value * step;
            step++;
        }
    
        printf("\n%d! = %llu\n",user_number,factorial_value);
    
        return 0;
    }