Question

java programming. One. Write a method public boolean hasOnlyoddDigits(int n) that returns true if its parameter n contains only odd digits (1, 3, 5, 7, or 9), and returns false otherwise. Zero is counted as an even digit whenever it appears inside the number. Remember that for a positive integer n, the expression (n % 10) gives its last digit, and the expression (n / 10) gives its other digits. Correct answer true false false false 357199 7540573 97531000

0 0
Add a comment Improve this question Transcribed image text
Answer #1
public class OddDigits {

    public static boolean hasOnlyOddDigits(int n) {
        if (n < 0) {
            n = -n;
        } else if(n == 0) {
            return false;
        }
        while (n > 0) {
            if((n % 10) % 2 == 0) {
                return false;
            }
            n /= 10;
        }
        return true;
    }

    public static void main(String[] args) {
        System.out.println(hasOnlyOddDigits(357199));
        System.out.println(hasOnlyOddDigits(0));
        System.out.println(hasOnlyOddDigits(-7540573));
        System.out.println(hasOnlyOddDigits(97531000));
    }

}

false false false Process finished with e xit code 0

Add a comment
Know the answer?
Add Answer to:
java programming. One. Write a method public boolean hasOnlyoddDigits(int n) that returns true if its parameter...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT