why is my code here not working im trying to print out every second number in the sequence as hello
public class Generalizations {
public static void main(String[] args) {
for(int i=0;i<10;i++)
System.out.println(i);
if (i%2==0){
System.out.println("hello");
}
}
}
To print every second element, print inside the condition as shown below:
for(int i=0;i<10;i++) {//esnure the brace here
if (i%2 == 0) {
System.out.println(i);//prints every second element
}
}
You missed the curly braces of for loop.
public class Generalizations {
public static void main(String[] args) {
for(int i=0;i<10;i++){
System.out.println(i);
if (i%2==0){
System.out.println("hello");
}
}
}
}
use this
public class Generalizations {
public static void main(String[] args) {
for(int i=0;i<10;i++){
System.out.println(i);
if (i%2==0){
System.out.println("hello");
}
}
}