creturn-valuefactorialfunction-definitionfunction-call

My factorial function is not returning factorial


I am not able to find out why my function returns the user input only rather then the factorial of the input.

#include <stdio.h>
#include <math.h>
int factorial(int x)
{
    //int x;
    int sum = 1;
    while (x!=0){
        sum = sum * x;
        x--;
    }
    return sum;
}

int main(){
    int x;
    printf("Enter value of x: ");
    scanf("%i",&x);
    factorial(x);
    printf("sum is %i", x);
    
    return 0;
}

Solution

  • Your factorial function does return a new value, but then you don't actually use that value.

    printf("sum is %i\n", factorial(x));