+ 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
3 ответов
+ 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.
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");
}
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 );