programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Liutauras Vilda
• Bear Bibeault
• Paul Clapham
• Jeanne Boyarsky
Sheriffs:
• Devaka Cooray
• Junilu Lacar
• Tim Cooke
Saloon Keepers:
• Tim Moores
• Ron McLeod
• Tim Holloway
• Claude Moore
• Stephan van Hulst
Bartenders:
• Winston Gutkowski
• Carey Brown
• Frits Walraven

# How can get my code to recognize that it only has even numbers?

Greenhorn
Posts: 12
Hey everyone,

I'm trying to make a code (Without the use of arrays.) when a user inputs a number (integer) , it will come out true if all the integers are even (0 is counted as even.) For example, if i input 224468 it will output true and if i input 2540 it will output false.

i haven't created my code but i have an idea of how i want it to go.

public boolean odds (int e)
set a counter
make it go through a for loop
as the first number goes in the for loop, it will then go through an if statement (n%2) and if that number comes out greater than 0,  counter goes up by 1. if it doesn't, counter does not increase.
repeat process until all numbers have passed though the loop.

finally, if the counter is greater than 0, return false and if counter is equal to 0 then come true.

I hope this is possible.

Any suggestions as to how i can tackle this? Examples would be really appreciated too.

Rancher
Posts: 3080
108
Hi Allen,

welcome to the ranch and enjoy the stay!

Yes, it sounds allright. Problem to solve is getting the individual digits. Do you have an idea how to do that? And o yeah, as soon as you find an odd digit, you can stop the process. Why?

Marshal
Posts: 63305
205
Welcome to the Ranch again

Why are you using a counter? Why not stop the loop as soonn as an odd digit is found?

zack redmen
Greenhorn
Posts: 12

Piet Souris wrote:Hi Allen,

welcome to the ranch and enjoy the stay!

Yes, it sounds allright. Problem to solve is getting the individual digits. Do you have an idea how to do that? And o yeah, as soon as you find an odd digit, you can stop the process. Why?

As I was creating my code  I decided to changed my idea and used a while loop. Basically when the an integer "n" is inputted, the while loop will go through the statement ( n%10 to retrieve the last digit, then %2 to determine if it's even or odd, then /10)  and verify it's true. If  it notices it's not true, it'll break the loop and come out as false.

Here's my code

From what I understand, the while loop will pass in each number into the statement, for example 2450,  one at a time to verify all the numbers meet the condition. So the 2 will pass as it is even then go to the next number, 4, and repeat the process and once it reaches 5, it will notice it doesn't mean the condition then break the loop and return false.

I thought my code would work, but as i tried simple numbers such as 0,1,2,3,4 ,5 it would only return true. I think I might have messed up on the modulus arithmetic part. How can I fix this?

zack redmen
Greenhorn
Posts: 12

Campbell Ritchie wrote:Welcome to the Ranch again

Why are you using a counter? Why not stop the loop as soonn as an odd digit is found?

Thank you so much I'm glad to have joined! Just so I dont sound repetitive, or be viewed as a spammer, please look at the post below yours.  I replied to the person's question which i believe is similar to what you ask.

Piet Souris
Rancher
Posts: 3080
108
• 1
Indeed it was similar.

It would be handy to assign each subcalculation to a variable, so that you can print out that variable to check if it is what you expect it to be.
So in Even, you could write

Then try n = 5 and see if Even reports an even or an odd result.

Campbell Ritchie
Marshal
Posts: 63305
205
I didn't see that post before replying. I am afraid I don't like that solution. For one thing you are repeating code; your expression is much too complicated. For another, there is something wrong with writing while (true).... Also I think your formula contains a serious logic error. No, I think there are two errors. Sorry.

Also the method name shoudn't start with a capital letter; I think isEven would be better.

zack redmen
Greenhorn
Posts: 12

Campbell Ritchie wrote:I didn't see that post before replying. I am afraid I don't like that solution. For one thing you are repeating code; your expression is much too complicated. For another, there is something wrong with writing while (true).... Also I think your formula contains a serious logic error. No, I think there are two errors. Sorry.

Also the method name shoudn't start with a capital letter; I think isEven would be better.

Can you provide some insight on what my mistake is and what i should do? Or how you would approach it?

zack redmen
Greenhorn
Posts: 12

Piet Souris wrote:Indeed it was similar.

It would be handy to assign each subcalculation to a variable, so that you can print out that variable to check if it is what you expect it to be.
So in Even, you could write

Then try n = 5 and see if Even reports an even or an odd result.

I've been informed that there are a few logical errors in my code. I would like to ask how you would tackle this, and if possible, show examples as well. I'm still new at coding and i do want to get better.

Piet Souris
Rancher
Posts: 3080
108
Since we need to test all digits of some  number, I would start thinking about a way to get all those digits. You are using n % 10, that gives the last digit. So far so good. But how next to obtain the last but one? If you have this, then you're almost done.

Something about programming style. I agree with  Campbell that 'isEven'  is a better name than just 'Even'. The difference being that when you call this method, then 'if (isEven(n)) ...' just reads a bit more pleasant than just 'if (Even (n)) ...' , but admittedly this is subjective.

Then, it is important to be very clear about what a method does and what parameters are involved. Look again at 'Even'. Does this method tests the input for being even, or should it test if all digits of the input are even? And does it test any number, or just a single digit? If a single digit, then why not call the method 'isEvenDigit?

But don't worry, it will come with increasing knowledge and experience, give it some time.

Marshal
Posts: 6574
443
@OP
Maybe having structure similar to this would help to you decompose task to smaller tasks?

example:

Campbell Ritchie
Marshal
Posts: 63305
205
We don't give out complete answers; that would deprive you of the chance to learn for yourself.
What you were doing is dividing a small number. You worked out the remainder when divided by 10 and then divided that remainder by 10. Had you followed PS's suggestion, you would have seen something was going wrong; it would have shown you the result of that division. I shall let you work out what the result of that division will be.
And Liutauras is right that you should divide the task into small parts, which will be easier to implement separately.

zack redmen
Greenhorn
Posts: 12
Hey everyone!

I'm just not sure how I can put this into a code.

Situation- Check to see if user input has all even numbers.
Logic:
Check if input number is even (x % 2) --> if even, proceed to shorten the number (x /10) --> check number if it is even (x %2) -->...repeat until it reaches the end of the number. Thereby letting us know all numbers are even.
If any number was odd, we know that the input number does not have all even numbers.

So it would look like this
Odd number scenario:
User input 12324--> Is even,true --> 1232 was shorten --> is even, true --> 123 was shorten --> is even, false.--> return false
Even number scenario:
User input 44--> Is even,true --> 4 was shorten --> is even, true --> return true

I've decided to use a do-while loop as it will execute the body and then do the condition.

Here's what I have.

I have tested this and well it sort of works. When you pass in a number with an odd number at the end, it will come out false. Which is good but, once there's an even number at the end or any even number like 2, I think it goes into an infinite loop.

I know a few problems it's just I'm not sure on how execute them properly/correct them.
For one I think it's doing an infinite loops because of  d==0. If i input an even number like 32, it will keep looping because (32%2=0)  meaning it's not going into (n/10) to get to the next number.

So i guess my question is, if the number , for example 32,  is inputted, I know my program will recognize it as Even however, how do i get it to recognize that it's Even and then pass it into (n/10) so that 32 can be 3 and then go into the loop again (By then the program will notice it's odd and come out false.) ?

Rancher
Posts: 3915
45
Are there rules as to how you can and cannot calculate this?

Just wondering if you would be allowed to use a regex, for example.
Or just plain process the user input as a String, maybe.

If you have to work with an int in the sort of way you've written, rather than a do...while, recursion is an option.

Anyway, for  the current solution, it's not obvious how this is supposed to exit.

I would expect more processing inside the loop.
'd' never gets reassigned, for example.

I would also expect there to be a return false in there if e%2 is not zero...after all, no point continuing once you have found an odd digit.

zack redmen
Greenhorn
Posts: 12

Tim Cooke wrote:zack redmen,
I have merged your topic into this topic. I hope that helps.

ohh thank you! I was kind of hesitant if i should continue with this thread or make anew one. thank oyu

zack redmen
Greenhorn
Posts: 12

Dave Tolls wrote:Are there rules as to how you can and cannot calculate this?

Just wondering if you would be allowed to use a regex, for example.
Or just plain process the user input as a String, maybe.

If you have to work with an int in the sort of way you've written, rather than a do...while, recursion is an option.

Anyway, for  the current solution, it's not obvious how this is supposed to exit.

I would expect more processing inside the loop.
'd' never gets reassigned, for example.

I would also expect there to be a return false in there if e%2 is not zero...after all, no point continuing once you have found an odd digit.

The only restrictions are to use only things we've learned so far (No arrays,  regex, sets as they haven't be covered yet.)

Ahh so something like
?

Campbell Ritchie
Marshal
Posts: 63305
205
• 1

zack redmen wrote:. . .  well it sort of works. . . . .

I am afraid it doesn't look like the sort of code that will work.

At this point I think there is some equipment interfering with your thought processes. Your computer. Turn it off. Revert to carbon‑based technology: pencil paper and eraser. Get a big eraser because you will use it a lot. Write down in words of one syllable how you are going to separate a number into its individual digits. Remember you may not need to store any of the digits. If any of what you write looks like Java® code, erase it and start again. When you have got it simple enough for me to understand, show us what you think the logic is.

 There’s no place like 127.0.0.1. But I'll always remember this tiny ad: Become a Java guru with IntelliJ IDEA https://www.jetbrains.com/idea/