recursive Function, to find even or odd digits inside given number

Faustino Da Silva

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));
}
kei-g

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Java, even number of even digits, odd number of odd digits

Given a number n find the prime number closest to it with maximum even digits

Tried to find if a number entered by a user is odd or even, but it given invalid syntax

Given no modulus or if even/odd function, how would one check for an odd or even number?

Count odd digits of a number with recursive method

How to check if consecutive digits in a number are even or odd?

Sum of odd and even digits of a number using recursion

Creating a recursive function that removes odd digits of an integer

Writing recursive function to identify even/odd

Segmentation fault in odd/even recursive function

ODD and EVEN Digits

I want to find count of even and odd digits in number. I have written below code for that. But somehow it is not entering the for loop

Recursive function to count the amount of digits in a number

a recursive function to calculate sum of a number digits

C - Find Numbers with Even Number of Digits

Arrays: Find Numbers with Even Number of Digits

Recursive method that finds the difference between the number of digits in 2 given ints

Problem in program to input a 3 digit number and check which digits are even and replace it with odd digits

Recursive function to check if all digits in an int variable are even

Flipping the sign of a number inside of a for loop based on whether the iterator is odd or even

Recursive function - count number of even numbers in tuple

Find smallest number formed by two digits divisible by given number

Given a number, find the next higher number with unique digits but 0 and 2

Find the smallest number whose digits multiply to a given number

Find minimum number of digits required to make a given number

Program that determines if a number is odd or even shows the wrong output if number is longer than 5 digits

Recursive function to check if a given number is Fibonacci

Understanding recursive odd/even functions

Problem with the program to check whether every even index contains an even number and every odd index contains odd number of a given list