This program you will learn how to use both conditional and looping constructs in a nested format, that is a conditional statement would be present inside a for loop. As mentioned in other articles, there would be several ways to write logic for any problem / program, here I am showing some of the simple ways to solve the program.
Way 1 – using a “for” loop
import java.lang.*; public class PrintEvenNumbers { public static void main(String rs[ ] ) { int targetEvenCount = 10; int currEvenCount = 0; // the below for loop will continue to run as long as the number of // even numbers found are less than the target number of even numbers // you want to print. for(int i=1;currEvenCountWay 2 - using a "while" loop
import java.lang.*; public class PrintEvenNumbers { public static void main(String rs[ ] ) { int i = 1; int targetEvenCount = 10; int currEvenCount = 0; // the below while loop will continue to run as long as the number of // even numbers found are less than the target number of even numbers // you want to print. while(currEvenCountThere are more ways, by creating a class and methods which can be called, the above program is also a way of writing a class writing the logic in a main method, but the predefined main method cannot be called by other classes. You may have to give a different method declaration other than the default main method provided by java. You will learn this in the tutorials which talks about creating classes and methods in java.
Please feel free to share your queries in form of comments, we would be more than happy to respond.