Please debug the snippet. This is not generating even number | Selenium Forum
M
Posted on 23/01/2017
The 2nd piece of code is not generating even number

public class evennumbersgenerate {

public static void main(String[] args) { //1st piece of code is working for printing even numbers
evengenerate(0,100);
//System.out.println();
/*for(int x=0;x<100;x++){
if(x%2==0){
System.out.print(" " +x);
}
}
}*/
public static void evengenerate(int x,int y){ //2nd piece,The 2nd piece of code is not generating even number
for(x=0;x<y;x++){
if(x%2==0){
System.out.print(" " +x);
}
}
}
}

M
Replied on 23/01/2017

Try to execute this once again, by uncommenting code in "Main" function.

It seems you commented out closed brace ("}") of "Main" function


M
Replied on 25/01/2017

are you getting any error?


M
Replied on 25/01/2017

i made it..

public class first1 {
static int sum=0;
public static void main(String[] args) { //1st piece of code is working for printing even numbers
int z= evengenerate(0,50);
System.out.println();
System.out.println("Return value of sum is: " +z);
if (z>1000)
{
System.out.println("Greater than 1000: " +z);
} else
System.out.println("Lesser than 1000: " +z);

/*for(int x=0;x<100;x++){
if(x%2==0){
System.out.print(" " +x);
}
}*/
}
public static int evengenerate(int x,int y){ //2nd piece
for(x=0;x<y;x++){
if(x%2==0){
System.out.print(" " +x);
sum=sum+x;
}
}
System.out.println(" ");
System.out.print("Total sum of all Even numbers is: " +sum);
return sum;
}
}