Looping Sederhana

Ada yg tau bikin loop seperti ini di java / php gimana

1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6

Jika input 4 maka kolomnya 4 row nya 4

avatar bimasp
@bimasp

1 Kontribusi 0 Poin

Diperbarui 5 tahun yang lalu

2 Jawaban:

Jawaban Terpilih

<pre> &lt;?php

$input = 5; $max = 20; $count = 0; $star = 1; for ($x = 1; $x &lt;= $max; $x++) { $count++; if($count != $input){ echo $star; }else{ echo $star."&lt;br&gt;"; $count = 0; } $star++; if($star == 10){ $star = 0; } }

//Output // 12345 // 67890 // 12345 // 67890 </pre>

avatar dedeardiansya
@dedeardiansya

40 Kontribusi 14 Poin

Dipost 5 tahun yang lalu

// java source code

public class tc {

public static void main(String arg[]) {
    java.util.Scanner input = new java.util.Scanner(System.in);
    System.out.print(&quot;Masukan jumlah baris &amp; kolom = &quot;);
    int a = input.nextInt();
    
    int[][] b = new int[a][a];
    int[] c = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
    
    int k=0;
    for(int i = 0; i&lt;b.length; i++){
        for (int j = 0; j&lt;b.length; j++){
            if(k==9){
                k=0;
            }
            else{
            b[i][j]=c[k];
            ++k;
            }
        }
    }
    
    for(int i = 0; i&lt;b.length; i++){
        for (int j = 0; j&lt;b.length; j++){
            System.out.print(b[i][j]);
        }
        System.out.print(&quot;\n&quot;);
    }
    
}

}

/* output:

Masukan jumlah baris & kolom = 4 1234 5678 9012 3456

*/

avatar Randomly
@Randomly

7 Kontribusi 5 Poin

Dipost 5 tahun yang lalu

Login untuk ikut Jawaban