Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

what was the error

+18 votes
asked Sep 24, 2019 by Gopi Jonnalagadda (250 points)
#include <stdio.h>
int fact(int )
int main()
{
    int x;
    printf("enter the number x");
    scanf("%d",&x);
    int res=fact(x);
    printf("the factorial of %d is%d",x,res);
    return(0);
    

}
int fact(int n)
{
    if(n==1)
    return 1;
    else
    return n*f(n-1);
}

19 Answers

+6 votes
answered Sep 24, 2019 by anonymous
#include <stdio.h>
int fact(int);
int main()
{
    int x;
    printf("enter the number x");
    scanf("%d",&x);
     int res=fact(x);
    printf("the factorial of %d is %d",x,res);
    return(0);
}
int fact(int n)
{
    if(n==1)
    return 1;
    else
    return n*fact(n-1);
}
0 votes
answered Sep 25, 2019 by anonymous

#include <stdio.h>
int fact(int );
int main()
{
    int x;
    printf("enter the number x");
    scanf("%d",&x);
    int res=fact(x);
    printf("the factorial of %d is%d",x,res);
    return(0);
    

}
int fact(int n)
{
    if(n==1)
    return 1;
    else
    return n*f(n-1);
}

–2 votes
answered Sep 25, 2019 by Uday Kiran Ch (110 points)
res is not declared
0 votes
answered Sep 26, 2019 by uday
in the else part the decleration of fact function is wrong

#include <stdio.h>
int fact(int );
int main()
{
    int x;
    printf("enter the number x");
    scanf("%d",&x);
    int res=fact(x);
    printf("the factorial of %d is%d",x,res);
    return(0);
   

}
int fact(int n)
{
    if(n==1)
    return 1;
    else
    return n*fact(n-1);
}
0 votes
answered Sep 26, 2019 by udaybhaskar (140 points)
/the decleration of fact function in else part was incorrect/

#include <stdio.h>
int fact(int );
int main()
{
    int x;
    printf("enter the number x");
    scanf("%d",&x);
    int res=fact(x);
    printf("the factorial of %d is%d",x,res);
    return(0);
   

}
int fact(int n)
{
    if(n==1)
    return 1;
    else
    return n*fact(n-1);
}
0 votes
answered Sep 26, 2019 by Lokendra
Semicolon in line 2
0 votes
answered Sep 27, 2019 by anonymous
the function is not declared when it is ouside the main
0 votes
answered Sep 27, 2019 by Edwin PA2LVD
On line 2 : missing semi collon ";"

You appear to be doing a recursive function, but function "f()" does not exist
Maybe you want to do

return n * fact( n - 1 );

program does not detect overflow of integer.

Any input > 12 will overflow and return invalid result (on my 64 bit i7 machine running linux-64-bit)

Since factorials of integers are constant program had been been written like this, much easier to verify and much more efficient in runtime :

==========================

#include <stdio.h>
  
int fact(int); // forward declaration

int main(int argc, char **argv)
{
    int x = 0;
    printf("enter the number x: ");
    scanf("%d", &x);
    int res = fact(x);
    if (res == -1)
    {
        printf("value %d is out of range too large\n", x);
    }
    else if (res == -2)
    {
        printf("value %d is out of range : negative\n", x);
    }
    else
    {
        printf("the factorial of %d is %d\n", x, res);
    }
    return(0);
}

int fact(int n) // return factorial of integer, negative on invalid input
{

    const int results[] = {
        1,
        1,
        2,
        6,
        24,
        120,
        720,
        5040,
        40320,
        362880L,
        3628800L,
        39916800L,
        479001600L,
       // 6227020800L,
       // 87178291200L,
       // 1307674368000L,
       // 20922789888000L,
    };

    if (n < 0)
        return -2;
    if (n > 12)
        return -1;

    return results[n];
}
0 votes
answered Sep 28, 2019 by KoushikKaleru (220 points)
Just the semicolon after declaring the function in the second line

int fact(int );
+1 vote
answered Sep 29, 2019 by anonymous

#include <stdio.h>
int fact(int );
int main()
{
    int x;
    printf("enter the number x");
    scanf("%d",&x);
    int res=fact(x);
    printf("the factorial of %d is%d",x,res);
    return(0);
    

}
int fact(int n)
{
    if(n==1)
    return 1;
    else
    return n*f(n-1);
}

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...