English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories
1

How do I solve this program????I tried a lot but was unsuccessful....could anybody solve it for me??

PROGRAM:Read odd numbers into an array till the user enters 0.Store all prime numbers in "prime" array and non-prime numbers in a separate array.Display the total number of numbers and prime numbers entered.

2007-03-14 17:34:54 · 3 answers · asked by kri91-16 2 in Computers & Internet Programming & Design

3 answers

This problem of prime numbers is not quite as easy as it seems, at least to code. As you know a prime number is any number that cannot be broken into any other numbers other than 1 and itself (with 1 not being prime or composite but a unit). Your best bet is to code something that tells you that the number is not prime by using something such as the Fermat test. The tests for a prime number are known as primality tests and can be a bit more complicated. I would just Google these to see what you can come up with.

As for odd numbers, just take the number and mod by 2, e.g. if(x%2 != 0) return odd; else return even;

Finally, just put these into arrays, such as int odd[] = new int[]; Or you could use an ArrayList<>, etc.

Good luck!

2007-03-14 17:46:13 · answer #1 · answered by Christina 2 · 1 0

I have worked this in C, i think it works well in java to.
Get the number as integer and check it is prime or not, if it is then store it in a prime array else store it in the other. finally you can check the size of the array to grt the total numbers you entered.

2007-03-14 17:46:34 · answer #2 · answered by Malert Jones 2 · 0 0

visit http://www.studyjava.org

and try

2007-03-15 02:19:43 · answer #3 · answered by ihoston 3 · 0 0

fedest.com, questions and answers