+ 2

Print all even number and odd number using one for loop?

let the loop going to 98 and make condition that return the loop down and print the odd number

26th Mar 2017, 11:34 PM
Turf
Turf - avatar
3 Answers
+ 2
thanks for your answer bro. but what I'm trying to do is to make the loop print all even numbers firstable. and then return again and print all odd numbers . sperated. you know what I mean.
28th Mar 2017, 12:15 AM
Turf
Turf - avatar
0
you can use two var for odd number and even number like this: for(int i=0,j=1;i<=98;i+=2,j+=2){ System.out.println(i + " is even"); System.out.println(j + " is odd"); } or use one var and mod operating like this: for(int i=0,;i<=98;i++){ if(i%2==0) System.out.println(i + " is even"); else System.out.println(i + " is odd"); }
26th Mar 2017, 11:41 PM
Saeed
Saeed - avatar
0
StringBuilder even=new StringBuilder(); StringBuilder odd=new StringBuilder(); for(int i=0,j=1;i<=98;i+=2,j+=2){ even.append(i + ","); odd.append(j + ","); } System.out.println("even : " + even ); System.out.println("odd : " + odd );
28th Mar 2017, 12:27 AM
Saeed
Saeed - avatar