0
i want to ask how to make this program with looping 1 4 3 8 5 12 7 16....100
6 Réponses
+ 2
Vaisakhs solution without the variable f:
for(int i = 1; i < 25; i++){
if (i % 2 != 0){
System.out.print(i+" ");
} else {
System.out.print(4*(i/2) + " ");
}
}
+ 1
@Robobrine
4*(i/2)=2*i 😇
+ 1
Surely it doesn't matter on the end output
8*i/4=16*i/4=2*i
But 2*i should be bit better and faster.
0
This can solve your problem
public class Program
{
public static void main(String[] args) {
int f=0;
for(int i=1;i<=50;i++){
if (i%2!=0){
System.out.print(i+" ");
f=f+4;
}else
System.out.print(f + " ");
}
}
}
0
thank you very much
0
Yes, but this way you can still see Vaisakhs solution with the f += 4 every second time you loop. Well, it doesn't really matter in the end, both notations do the same thing.