DEV Community

gopinathvarad
gopinathvarad

Posted on

Haskell program answer please

Define a function dropOdds :: Int -> Int with the following behaviour.
For any positive number m, dropOdds m is got by dropping all the odd digits
in m. (If all the digits in the number are odd, the answer should be 0.)

Test cases:
dropOdds 0 = 0
dropOdds 8 = 8
dropOdds 1357 = 0

Discussion (2)

Collapse
shashwatseth profile image
Shashwat Seth

dropOdds :: Int -> Int
dropOdds x
| x > 10 = 10 * dropOdds restDigits + dropOdds firstDigit
| otherwise = dropOddsOneDigit x
where
firstDigit = x mod 10
restDigits = x div 10

dropOddsOneDigit x
| x mod 2 == 0 = x
| otherwise = 0

Collapse
shashwatseth profile image
Shashwat Seth

Are you ennrolled in the NPTEL course?