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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
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;currEvenCount<targetEvenCount;i++) { // This is condition which finds if current value in i is an even or not // if its an even number, currEvenCount is incremented by 1. if(i%2==0) { currEvenCount++; System.out.println("Even Number :" + i); } } } } |
Way 2 – using a “while” loop
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
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(currEvenCount<targetEvenCount;) { // This is condition which finds if current value in i is an even or not // if its an even number, currEvenCount is incremented by 1. if(i%2==0) { currEvenCount++; System.out.println("Even Number :" + i); } i = i + 1; // increment the value of i to find the next even number } } } |
There 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.
Founder of TestingTools.co, constantly shares knowledge on different test automation tools. Has experience in building proof of concepts, solutions, frameworks, platforms & implementation of test automation projects.
In pursuit of building a platform with inbuilt framework and reusable components for Oracle Cloud applications ( Oracle HCM, CX, SCM, Financials clouds, Salesforce and other cloud applications )
Major accomplishments in his career:
- Product architect and development manager for test automation platforms
- Oracle Flow Builder @ Oracle
- CloudTestr @ Suneratech
- 2 times to oracle open world
- More than 20 successful POCs ( Proof of concepts )
- 100 demos
- Designed pricing models.
- Trained more than 50 members.
- Built more than 100 re-usable functions.
Worked with tools: