Design and implement an application that determines and prints the number of odd, even, and zero digits in an integer value read from the keyboard. (java)
using for loop

Recommended Answers

All 7 Replies

You just copy/pasted your assignment without even a moment taken to explain what help you need. That's highly disrespectful to the many people who give their time to help others here.

There are lots of people here who will freely give their time to help you become the best Java programmer you can be. There's nobody here who is interested in helping you cheat or doing your homework for you.

DaniWeb Member Rules (which you agreed to when you signed up) include:
"Do provide evidence of having done some work yourself if posting questions from school or work assignments"
http://www.daniweb.com/community/rules

Post what you have done so far and someone will help you from there.

Well, thanks for spending your precious time and yea i copy pasted it, it is my assignment but let me tell you i tried my best before posting it and never meant to cheat. I really tried many ways of doing it but needed help. But yes sorry i didn't mention what part i was stuck at.
thanks

Did you try google? https://www.google.com/search?q=Design+and+implement+an+application+that+determines+and+prints+the+number+of+odd%2C+even%2C+and+zero+digits+in+an+integer+value+read+from+the+keyboard.+%28j&ie=utf-8&oe=utf-8 finds working code from this old homework and you can rewrite the while to be a for loop.

You pasted the class assignment but need to show your work. Asking for the code is the second part of the assignment. The first part is design. Tell us your design.

import java.util.Scanner;
public class NumEvenOddZero {
    public static void main(String[] args){
        int odds=0;
        int evens=0;
        int zeros=0;
        Scanner kbd=new Scanner(System.in);
        System.out.println("Enter an integer: ");
        int input=kbd.nextInt();
        String s=""+input;
        int length=s.length();

        for(int i=0; i<length; i++){



            int x=input%10;
            input/=10;

            if(x==0){
                zeros++;
            }else if(x%2==0){
                evens++;
            }else{
                odds++;
            }


        }
        System.out.println("Number of odds: "+odds);
        System.out.println("Number of evens: "+evens);
        System.out.println("Number of zeros: "+zeros);
    }
}

this is what i did but when i run it, it doesn't work for zero!

It works Ok for zero when I run it.

input 0123 because when 0 is in the begining it doesnt work

If you need to count the leading zeroes, you can't use Scanner's nextInt by itself because it skips the leading zeroes. Either read the input in as a String and iterate through the String counting leading 0's, then converting the String to an Integer, or use Scanner's hasNext function along with Scanner's skip function in a while loop to go through the leading zeroes. Either way, you would be counting leading zeroes and incrementing your zeros variable.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.