I am searching for a java pattern that starts at 2, prints 2, skip 2, print 2, skip 2. After this, it should start at 4, print 4, skip 4, etc. After this it should start at 8, print 8, skip 8, etc. After this the same for 16, 32, 64, etc.
I would like to know the pattern for a Java program to print the following: 1, 3, 5, 7, 9, 11, 13, ... 2, 3, 6, 7, 10, 11, 14, 15, ... 4, 5, 6, 7, 12, 13, 14, 15, .... 8, 9, 10, 11, 12, 13, 14, 15, ...
I think this problem could be solved using nested for loops, so I came up with the following:
//this for loop makes sure the pattern starts at the right place, so: 1, 2, 4, 8, 16
for(int i = 1; i <= 128; i = i * 2){
//i think we need another for loops that starts at i = j
}
Aucun commentaire:
Enregistrer un commentaire