-1

Can someone please explain to me how does the modulus work if you wanna print set of numbers per line like in the given code below? I only know that the role of the modulus is to give the remainder of a number and that's about it. I would like to know the process it goes through.

int []arrayLoterry= new int[50];
String output="";

   for(int i=0;i<arrayLoterry.length;i++){
      arrayLoterry[i]=(int) Math.floor(Math.random()*49)+i;
      output+=arrayLoterry[i]+" ";

      if(i%10==9){
         output+="\n";
      }
   }

System.out.println(output);
Blue Eagle
  • 55
  • 8
  • 3
    Possible duplicate of [How does the modulus operator work?](http://stackoverflow.com/questions/12556946/how-does-the-modulus-operator-work) – MrZander May 17 '17 at 00:23
  • 1
    refer this [post](http://stackoverflow.com/questions/4403542/how-does-java-do-modulus-calculations-with-negative-numbers) it may be helpful. – Rajith Pemabandu May 17 '17 at 00:24
  • There is no modulus operator in Java. This is the [remainder operator](http://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.17.3). – user207421 May 17 '17 at 00:41

2 Answers2

2

The if condition simply checks if the remainder of i % 10 is 9 then performs the task within the if block. In regards to your code, basically, after every 10 numbers appended to the string output, a newline will be appended to the string output in order to separate the lines.

Example:

when i == 9 // append newline
when i == 19 // append newline
when i == 29 // append newline
when i == 39 // append newline
when i == 49 // append newline

meaning the end result will be a 5 x 10 matrix.

side note - I wouldn't recommend concatenating strings within a loop as that is a performance bottleneck, you're better off using a StringBuilder along with StringBuilder#append method.

Ousmane D.
  • 54,915
  • 8
  • 91
  • 126
1

This statement:

if(i%10==9){
    output+="\n";
}

Is saying: "If i divided by 10 leaves the remainder of 9, add "\n"(which is a new line) to variable output.

Moduli simply divide the first number by the second, and give the remainder. Examples below:

10%3 = 1  
20%2 = 0  
7%4 = 3

In your case, this will start a new lie after every 10 numbers. You might think, Why not change that statement to if(i%10 == 0)? The reason it is i%10 == 9 is because when the loop executes the first time, i holds the value of 0, so when it runs through the modulus, the answer is 0, which makes a new line after the first execution of the loop.

Since Java counts 0, 1, 2 like we count 1, 2, 3, we have to make the line break after everything that ends in 9. So a new line starts after the 9th(10th for us), 19th(20th for us), 29th(30th), 39th(40th), and 49th(50th and last) number outputted.

This chart shows how Java counts, versus how we count.

Java | 0 1 2 3 4 5 6 7 8 9 
----------------------------
Us!! | 1 2 3 4 5 6 7 8 9 10
  • @BlueEagle - [this wikipedia article](https://en.wikipedia.org/wiki/Modulo_operation) might help you to understand. –  May 17 '17 at 00:50
  • its a bit clear. But since if the answer of the remainder is 0 prints a new line, how come it doesn't print after the first number when we have (i%7==3) in the if? – Blue Eagle May 17 '17 at 01:45
  • When the answer is 0, it does not print a new line. it prints a new line when the remainder is 9 –  May 17 '17 at 02:01
  • I think your getting confused, there is no `(i%7==3)`, when the loop runs. the variable `i` refers to how many times the loop runs. The modulo gets the remainder of i/10 and if that = 9, it prints a new line. This will print a new line after every 10 numbers. –  May 17 '17 at 02:31
  • @BlueEagle to simplify, that `if` statement simply prints a new line after every ten numbers outputted. If this answer was the most helpful, please accept it :) –  May 18 '17 at 01:27