C | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

C

Help me in C #include <stdio.h> int noOfDigits(int n){ static int counter = 0; if (n == 0) return counter; else { counter++; noOfDigits(n/10); } } int main() { int num,fc; printf("Count the digits of a given num: \n "); printf(" input a num :"); scanf("%d", &num); fc = noOfDigits(num); printf("the num of digits on the num is %d \n", fc); return 0; }

3rd Dec 2021, 11:38 AM
Dee
Dee - avatar
6 Answers
+ 2
Your else statement in noOfDigits() should return a value as well, since your function is forced to return integer.
3rd Dec 2021, 11:45 AM
cadbrooke
cadbrooke - avatar
+ 2
Mention your code in codebit then share link here and give description. where u have define function int noOfDigit(int n) here function return type is int type if it is anything apart from void then u need to use return any value here return type is int then u need to return any integer value . #include <stdio.h> int noOfDigits(int n){ static int counter = 0; if (n == 0) return counter; else { counter++; noOfDigits(n/10); } return counter; } int main() { int num,fc; printf("Count the digits of a given num: \n "); printf(" input a num :"); scanf("%d", &num); fc = noOfDigits(num); printf("the num of digits on the num is %d \n", fc); return 0; }
3rd Dec 2021, 1:31 PM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 2
Dee , your code is very good made. There will be needed only an additional return statement in the else part as already Scoozak said. This is mandatory for non void functions. This can be done as here: https://code.sololearn.com/cdPB5bGP74oo/?ref=app
3rd Dec 2021, 1:36 PM
JaScript
JaScript - avatar
+ 1
With A?
3rd Dec 2021, 11:40 AM
cadbrooke
cadbrooke - avatar
0
why its wrong ?
3rd Dec 2021, 11:41 AM
Dee
Dee - avatar
0
what shoud i write ? return int ?
3rd Dec 2021, 11:47 AM
Dee
Dee - avatar