+ 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); }

29th Sep 2023, 6:18 PM
Hiriharan V M EEE
Hiriharan V M EEE - avatar
7 ответов
+ 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
29th Sep 2023, 7:13 PM
Darpan kesharwani🇮🇳[Inactive📚]
Darpan kesharwani🇮🇳[Inactive📚] - avatar
+ 2
Try again but with the second int fact(int n) with the ;
29th Sep 2023, 6:27 PM
Nicolás González
Nicolás González - avatar
+ 2
Thank u Darpan kesharwani🇮🇳 Your answer given me good satisfaction to Code... And good luck on ur programming journey♥️✨
1st Oct 2023, 9:09 PM
Hiriharan V M EEE
Hiriharan V M EEE - avatar
+ 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:
29th Sep 2023, 6:29 PM
D1M3
D1M3 - avatar
+ 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); }
29th Sep 2023, 6:30 PM
D1M3
D1M3 - avatar
+ 1
Thank u for reaching out ... I'll try it out
29th Sep 2023, 7:00 PM
Hiriharan V M EEE
Hiriharan V M EEE - avatar
0
Np
29th Sep 2023, 7:00 PM
D1M3
D1M3 - avatar