0

Guys help in this program

public class Even { void regular(int x,int y) { for(int i=x;i<=y;i++) { if(i%2==0) { System.out.println("the num bw 1 to 100"+i) ; }else { System.out.println("\n"); } } } } public static void main (String[] args) { Even d=new Even(); d.regular(1,100); }

9th Dec 2016, 2:55 PM
tharun
tharun - avatar
1 Réponse
+ 2
It looks like you are trying to print the even numbers between 1 and 100. 1) The last brace (}) before main() should be placed after main() so that main() is part of the class. 2) You might want to include a space at the end of the string to separate the even numbers from the 100. 3) You might even prefer to print the string "The even numbers between 1 and 100 are:" before the loop so it only appears once, not with each number printed. 4) Since 1 and 100 are parameters it would be better to print "The even numbers between " + x + " and " + y + " are:" Then the output will remain accurate if you change the bounds (1 and 100 at the moment.) 5) Java wiil automatically convert integers to strings when you concatenate (+) them to a string.
9th Dec 2016, 3:18 PM
Gordon Garmaise
Gordon Garmaise - avatar