Search code examples
cfactorial

Program fails when input is 10 and factorial value is returning upto 33 number only


When 10 is entered as input the program (for calculating factorials) fails, whereas for all other inputs (within system limits) the program works fine except when any character is entered it goes into infinite loop with output as "Factorial of 1 is 1".

If I enter 34 or any number greater than 34 then I am getting output as "factorial of [entered number] is 0". Why the factorial value is 0 for number greater than 33?

Here is the program:

 #include <stdio.h>
    #include <stdlib.h>

int display();
void fact_fun(int num_fact);

int main()
{
    int num = 0;
    char next;

    next=display();

    while( next != '\n')
    {
        num = next;
        fact_fun(num);
        next=display();
    }

    return 0;
}

int display()
{
    int input;
    printf("\nEnter number to find factorial or press ENTER to exit :");
    scanf("%d",&input);

    return input;
}

void fact_fun(int num_fact)
{
    int fact=1;
    if((num_fact>='a' && num_fact<='z') || (num_fact>='A' && num_fact<='Z'))
    {
        printf("Alphabet is not allowed");
    }
    else if(num_fact < 0)
    {
        printf("Negative number is not allowed");
    }
    else if(num_fact == 0)
    {
        printf("Factorial of %d is 1",num_fact);
    }
    else
    {
        for(int i = 1; i<= num_fact; i++)
        {
            fact = fact*i;
        }
        printf("Factorial of %d is %d",num_fact,fact);
    }
}

Kindly guide me where I am making mistake.


Solution

  • In display function you are using char instead of int as you are using %d format specifier in scanf, change it to int everywhere. You must have seen the warning while compiling:

    warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘char *’
    

    Apart from this, your program fails to deliver when number entered is 10, to solve it:

     while(next>=0) //WHEN NEGATIVE NUMBER IS ENTERED IT WILL END THE LOOP
     {
        num = next;
        fact_fun(num);
        next=display();
     }
    

    Simplify the fact_fun:

    void fact_fun(int num_fact)
    {
        int fact =1;
        if(num_fact == 0)
        {
            printf("Factorial of %d is 1",num_fact);
            return;
        }
        else
        {
            for(int i = 1; i<= num_fact; i++)
            {
                fact = fact*i;
            }
            printf("Factorial of %d is %d",num_fact,fact);
        }
    }
    

    Then it will work fine.

    REASON BEHIND FAILURE OF PROGRAM WHEN INPUT IS 10: The ASCII value of newline \n is also 10, so when ten is entered, the int value of \n is compared to value returned by display i.e. 10, hence the while loop is never executed in this case.

    Here is how a safe method of taking input should be (scanf is a disaster):

    int display()
    {
        char inp[10]={0};
        int  input;
        int index=0;
        printf("Enter number to find factorial or press ENTER to exit : ");
    
        while(((input=getchar())!=EOF)&(index<10))
        {
            if((input>='0')&&(input<='9'))
            {
                inp[index++]=input;
            }
            else if(input=='\n')
                break;
            else
                return -1;
        }
        input=atoi(inp);
    
        return input;
    }