Java Code
I don't know how to show odd or even digits from an integer number. For example I ask the user to enter a 4 digits integer number. For example the user enter 4875. I want to display odd digits (7 5) and even digits (4 8). By Extract each digit of the number using integer division and store the results in separate variables.
for example, that 4875 is 4*1000+8*100+7*10+5

Recommended Answers

All 5 Replies

Be creative, for example if you read input from user you will get string object. Convert string to character array, read array element by element, convert element to integer and fine out if it is odd or even number.

int a=0,b=0,c=0,d=0;
        int n=4875;
        int rest=0,i=1;


        while(n!=0)
        {   
           rest = n%10;//ghet the last digit of the number
           n=(n-rest)/10; //the number gets the last digit out
    

              switch(i)
              {
                  case 1:a=rest;break;
                  case 2:b=rest;break;
                  case 3:c=rest;break;
                  case 4:d=rest;break;
              }
              i++;
        }
//see what we got
        System.out.println(Integer.toString(a));
        System.out.println(Integer.toString(b));
        System.out.println(Integer.toString(c));
        System.out.println(Integer.toString(d));
//after you have the variables you can test if(variable%2==0) is odd else its even

Good luck, reminds me of my first year of Computer Science (i was coding this problem in C) :)

Can someone please tell me why I have been down-voted for previous post? I really wanted to solution this problem. I am used to explaint a lot of students facts like this so that's why I gave ki72 the answer! If I would have bad manners fo course I should have been down-voted.
I really don't understand. Someone please tell me. And maybe don't down-vote this post, too. Really. No disrespect intended.

  • Guess it will be down to the fact we have here nice rule - We only give homework help to those who show effort that you ignored.
  • Next OP already had two hints how to approach this problem, therefore your solution wasn't necessary.
  • Lastly our suggestions been better in comparison yours where if user provide number in different format then 4 digits program will not work correctly

Do you still believe that person who down-voted you made wrong decision?

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.