what was the error

+9 votes
asked Sep 24 by (260 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);
}

15 Answers

+4 votes
answered Sep 24 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 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);
}

–1 vote
answered Sep 25 by (130 points)
res is not declared
0 votes
answered Sep 26 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 by (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 by Lokendra
Semicolon in line 2
0 votes
answered Sep 27 by anonymous
the function is not declared when it is ouside the main
0 votes
answered Sep 27 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 by (220 points)
Just the semicolon after declaring the function in the second line

int fact(int );
0 votes
answered Sep 29 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.
...