0

i want to ask how to make this program with looping 1 4 3 8 5 12 7 16....100

29th Jan 2017, 2:10 AM
Kurby Lee
Kurby Lee - avatar
6 odpowiedzi
+ 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) + " "); } }
29th Jan 2017, 3:21 AM
Robobrine
Robobrine - avatar
+ 1
@Robobrine 4*(i/2)=2*i 😇
29th Jan 2017, 4:22 AM
Megatron
Megatron - avatar
+ 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.
29th Jan 2017, 4:29 AM
Megatron
Megatron - avatar
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 + " "); } } }
29th Jan 2017, 3:03 AM
Vaisakh
Vaisakh - avatar
0
thank you very much
29th Jan 2017, 3:30 AM
Kurby Lee
Kurby Lee - avatar
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.
29th Jan 2017, 4:26 AM
Robobrine
Robobrine - avatar