-
new to programming
I'm new with programming so I'd appreciate if you guys could help me out with this one.
I have to write a program consisting this:
Write a static method called countDivisors that figures out how many positive integers are divisors of the supplied int parameter. It should take in a single int parameter and return an int, indicating how many divisors there are (you can assume that the parameter supplied by the caller will always be a positive integer). For example, countDivisors(10) should result in 4, whereas countDivisors(9) should result in 3.
Since a prime number is (by definition) a positive int that has exactly 2 divisors, it should now be a straightforward matter to determine whether or not an int is a prime number. Using your countDivisors method, write a program that takes an int from the user, determines whether or not it is a prime number, and reports the number of divisors:
I don't have a clue what to do, any help would be appreciated. thank you
-
Re: new to programming
Well what have you done so far? Take things step-by-step and break down what you need to do. It's a good idea to start by writing down what the program is required to do without any coding first. The first part is asking to create a static method with an int parameter that finds out how many divisors a positive integer has. The second part asks to determine whether it is prime. Since it tells you what to do, try to think about how to go about doing this.
For example,
10 has 4 divisors: 1, 2, 5, 10
9 has 3 divisors: 1, 3, 9
8 has 4 divisors: 1, 2, 4, 8
7 has 2 divisors: 1, 7 <----- prime, because it has ONLY 2 divisors
etc...
Again, think about what you could use to determine this before actually coding anything. Once you do and have some code but are still stuck, then please post what you're still having trouble on.
-
Re: new to programming
alright, what I'm confused on though is how to write a code that determines how many divisors a number has
-
Re: new to programming
There is a math function called modulus that will do the work for you.
The Remainder or Modulus Operator in Java
-
Re: new to programming
see using array we can achieve this .i have a concept in my mind this time
see using mod operator, we can find out the modulus of the number and inserting that one in array side by side updating array each time .
Hope this would hep
-
Re: new to programming
-
Re: new to programming
@Rex, please read the forum rules and the following link: http://www.javaprogrammingforums.com...n-feeding.html