Hello! Tell me about the code?

Display all positive divisors of a natural number and count the number of divisors entered by the user from the keyboard.

The code seems to be running compiled.

1) Is the code correct for the positive dividers?

2) When compiled code displays the following:

Enter your natural number - 6 // entered number

The number 3 is divided by - 1 3 6 The number of dividers - 3

How can I make it so:

Enter your natural number - 6 // entered number

The number 3 is divided by - 1 3 6

The number of dividers - 3

How can you make the dividers appear in reverse order:

Enter your natural number - 6 // entered number

The number 3 is divided by - 6 3 1

The number of dividers - 3

3) How can the code be described, on an error, if the user entered a negative number?

4) How can the code be described, what cannot be divided into zero if the user entered 0?

public class Test { public static void main(String args[]){ int n; int count = 0; System.out.print("Вводите ваше натуральное число - "); Scanner sr = new Scanner(System.in); n = sr.nextInt(); System.out.print("Число " + n + " делится на - " ); for(int i = 1; i <= n; i++){ if ((n % i) == 0){ count++; System.out.print(i + " "); } } System.out.println("Количество делителей - " + count); } } 

Closed due to the fact that the question is too common for the participants Dmitriy Simushev , korytoff , torokhkun , Yuri Glushenkov , Streletz 7 Dec '15 at 14:32 .

Please correct the question so that it describes the specific problem with sufficient detail to determine the appropriate answer. Do not ask a few questions at once. See “How to ask a good question?” For clarification. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • At 0 you will never share, so that the user does not enter (look at your code). But to check whether the user entered a natural number (and report an error (and do not fall down when entering not a number)) does not interfere. - avp

2 answers 2

  1. In general, we can assume that is correct. Only one unit, why is it here? And so it is known that any number is divided by one and divided by itself.
  2. So print the line after the cycle. For example, System.out.println (); To show them in reverse order .. so make a cycle in the opposite direction.
  3. So take and check the entered number before entering the cycle. Think about it. How would you do if you performed the task manually? They call you a number, and first of all you think, and what did they tell you? Have you been given a reasonable number? If not, then tell the person that he is a fool. If the number is normal, then go already to sort through the options of numbers.
  4. Same story. If a person tells you zero, then you tell him that he is a fool. The program acts the same, only politely.
  • Thanks for the answer "cy6erGn0m" - turtles

It is better to cycle through i to [sqrt(n)] and output the divisor along with the private (except when they are equal). Sharply accelerates the algorithm for large n.