+ 1
To check given decimal number has 0 and 1 in java program
Hi friends I need a java program to check given decimal number has 0 and 1 only. Please help me if someone knows the answer.
2 Answers
+ 5
Turn the number into an array of characters and search through the array for '0' or '1'.
+ 5
Please do not write a sentence in the Relevant Tags. Words in tags are used by search engine, and the presence of irrelevant words will break the search functionality, it will be difficult then to search things : )
Why not try to make your version first, then you can share your code in Description for everyone to see (if you need help with it).
Asking for ready to use code is no good for you, you will miss the chance to learn. Asking others to help on something you have a problem with is okay though.
I'm gonna give you some hints (Assume <n> is the number)
- Prepare int variable e.g. <zero> and <one>, set their value to zero.
- Setup a loop, then within the loop body ...
- Use modulo operator % to get each digit of the number by obtaining the remainder of <n> % 10.
- See if the remainder is 0 or 1. If it's 0 - set <zero> to 1, and if it's 1 - set <one> to 1.
- See if value of both <zero> and <one> equal to 1. If their values are both 1 - exit the loop; otherwise divide <n> by 10
Go give it a try! đ