Thread: How to count odd digits in unsigned long number in C

  1. #1
    Registered User
    Join Date
    Jan 2018
    Posts
    1

    How to count odd digits in unsigned long number in C

    I have a question at school and I got stuck. I need to write a recursive program in c:
    Code:
    int check_odd(unsigned long n){ }

    the function getting a positive natural parameter and the function need to check if this number has more than 3 odd different digits if there are more than 3 odd different digits the function need to return 0 else if there are less than 3 odd different digits the function return 1.
    I need to do that in recursion. hope someone can help me with that. thank you

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,616
    You said you got stuck. What did you try so far? The problem with just posting an answer is that you're supposed to learn how to use the return value in recursion as storage, and it's against the rules to post an answer, so...

  3. #3
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    776
    What should the function return if there are exactly 3 odd digits?

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,035
    What should the function return for input of 5555 ?

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    Registered User
    Join Date
    Apr 2017
    Location
    Quetzaltenango
    Posts
    82
    You can check if last digit is odd with the modulus operator. You can change the last digit by dividing by ten.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 11-29-2013, 11:05 PM
  2. Replies: 7
    Last Post: 02-20-2013, 12:28 AM
  3. Replies: 3
    Last Post: 02-03-2013, 06:27 AM
  4. Count the number of vowels, consonants, digits etc.
    By kumar14878 in forum C Programming
    Replies: 3
    Last Post: 05-09-2005, 12:34 AM
  5. Count Number of Digits in and Integer
    By redneon in forum C++ Programming
    Replies: 2
    Last Post: 08-18-2003, 04:16 PM

Tags for this Thread