Home > Software design >  recursive Function, to find even or odd digits inside given number
recursive Function, to find even or odd digits inside given number

Time:11-17

Basically, its printing only one instance when it happens, and i don't understand why, maybe has something to do with the code reseting every time and starting the variable at 0 again, and i got another question if someone can help me with, i have to return both values when its odd and even, like how many digits are even and odd at the same time, i'm having a little trouble figuring out how to do it

#include <stdio.h>

int digits(int n) 
// function that checks if the given value is odd or even, and then add 
//   1 if it's even, or odd, it's supposed to return the value of the quantity 
// of digits of the number given by the main function
{
    int r;
    int odd  = 0;
    int even = 0;

    r = n % 10;
    if (r % 2 == 0) // check if given number is even
    {
        even = even   1;
    }
    if (r % 2 != 0) // check if its odd
    {
        odd = odd   1;
    }

    if (n != 0) {
        digits(n / 10); // supposed to reset function if n!=0 dividing
                // it by 10
    }
    if (n == 0) { return odd; }
}

int
main() // main function that sends a number to the recursive function
{
    int n;
    printf("type number in:\n ");
    scanf("%d", &n);

    printf("%d\n", digits(n));
}

CodePudding user response:

odd and even variables are local in your code, so they are initialized by zero every time. I think they should be declared at caller of the recursive function, or be declared as global variables.

#include <stdio.h>
void digits(int n, int *even, int *odd)//function
{
  int r;
  r = n % 10;
  if (r % 2 == 0)//check if given number is even
  {
    *even = *even   1;
  }
  else //otherwise, its odd
  {
    *odd = *odd   1;
  }

  n /= 10;
  if (n != 0)
  {
    digits(n, even, odd);//supposed to reset function if n!=0 dividing it by 10
  }
}

int main()
{
  int n, even = 0, odd = 0;
  printf("type number in:\n ");
  scanf("%d", &n);

  digits(n, &even, &odd);

  printf("even: %d\n", even);
  printf("odd: %d\n", odd);

  return 0;
}

CodePudding user response:

Maybe I found the problem you are facing. You you initialized you odd and even variable as zero. every time you call the function it redeclares their value to zero again. You can use pointer caller or use those as your global variable so that every time they don't repeat their initial values again.

CodePudding user response:

Implementing a function that counts the number of odd and even digits in a number, is not to be done using recursive. That is simply a wrong design choice.

But I assume that it's part of your assignment to use recursion so ... okay.

You want a function that can return two values. Well, in C you can't!! C only allows one return value. So you need another approach. The typical solution is to pass pointers to variables where the result is to be stored.

Here is the code:

void count_odd_even(const int n, int *even, int *odd)
{
    if (n == 0) return;

    if (((n % 10) % 2) == 1)
    {
        *odd  = 1;
    }
    else
    {
        *even  = 1;
    }
    count_odd_even(n/10, even, odd);
}

And call it like

int odd = 0;
int even = 0;
count_odd_even(1234567, &even, &odd);
  • Related