+ 1
Why the error is coming like ( the end of non void mentioning that ' } ' this bracket. Any corrections is there
#include <stdio.h> int fact(int n); int main() { int n; printf("Enter n: \n:"); scanf("%d",&n); printf("Factorial = %d",fact(n)); return 0; } int fact(int n) { if(n!=1) return n*fact(n-1); }
7 Antworten
+ 5
Hiriharan V M EEE,
Your code is correct but just little mistake is here,
According to your code,the compiler expects a return statement for all possible execution paths in the fact() function.
for fix error, add a return statement after the if statement, outside of the if block.
See this modified version..
https://code.sololearn.com/cBlta1O9hYGn/?ref=app
+ 2
Try again but with the second int fact(int n) with the ;
+ 2
Thank u Darpan kesharwani🇮🇳
Your answer given me good satisfaction to Code... And good luck on ur programming journey♥️✨
+ 1
The error you're encountering is because your fact function doesn't have a return statement for the case when n is equal to 1. In C, since your fact function is declared to return an int, you need to ensure that it returns an int value in all possible code paths. Here's a corrected version of your code:
+ 1
Here is the working Version :
#include <stdio.h>
int fact(int n);
int main()
{
int n;
printf("Enter n: ");
scanf("%d", &n);
printf("Factorial = %d", fact(n));
return 0;
}
int fact(int n)
{
if (n == 0 || n == 1)
return 1;
else
return n * fact(n - 1);
}
+ 1
Thank u for reaching out ... I'll try it out
0
Np