C Programming I have two codes that I need to figure out the

C Programming: I have two codes that I need to figure out the output. Would you mind showing how you got the answers

Thanks in advance


#1)
#include
int f(int num) {
if(num == 0) return 0;
int digit = num % 10;
if(digit % 2 == 1) return 1 + f(num/10);
return f(num/10);
}
int main() {
printf(\"25 is %d \ \", f(25));
printf(\"1010 is %d\ \", f(1010));
printf(\"2468 is %d\ \", f(2468));
printf(\"1357 is %d\ \", f(1357));
printf(\"12345 is %d\ \", f(12345));
return 0;
}

#2)
#include
#include
void f(char *s, int n) {
   if(*s == \'\\0\') return;
   printf(\"%c\", *s);
   if(strlen(s) > n)
   f(s + n, n);
return;
}
int main () {
   char s1[] = \"Crimson Tide\";
   char s2[] = \"University of Alabama\";
   char *ptr = s1;
   printf(\"%s\ \", s1);
   f(ptr, 1); printf(\"\ \");
   f(ptr, 2); printf(\"\ \");
   ptr = s2;
   printf(\"%s\ \", s2);
   f(ptr, 3); printf(\"\ \");
   return 0;
}

Solution

The output for the 1st is

25 is 1
1010 is 2
2468 is 0
1357 is 4
12345 is 3

int f(int num) {
if(num == 0) return 0;//not true
int digit = num % 10;//digit = 2
if(digit % 2 == 1) return 1 + f(num/10); //it will return 1
return f(num/10);

int f(int num) {
if(num == 0) return 0;//false
int digit = num % 10;//digit = 10
if(digit % 2 == 1) return 1 + f(num/10);//false
return f(num/10); //will call 101 same function

then again digit =1 it will return 1+f(10) whereas f(10) is 1

similarly you can calculate for other numbers

C Programming: I have two codes that I need to figure out the output. Would you mind showing how you got the answers Thanks in advance #1) #include int f(int nu
C Programming: I have two codes that I need to figure out the output. Would you mind showing how you got the answers Thanks in advance #1) #include int f(int nu

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site