You are here
Home > Technology > How Can You Print Even Numbers In Java Program?

# How Can You Print Even Numbers In Java Program? In this article, we shall learn how Java Programs work and how you can print all the even numbers from the starting to the given maximum number. You can utilize circling strategies, to repeat for each considerable number until a limit, or most extreme. We will use for circle and keeping in mind that circle to repeat over the even numbers up to we arrive at the end.

Or on the other hand, you can repeat from 1 to n, in strides of 1, and check to assume the number is in any event, during every cycle.

Allow us to compose calculations for these two methodologies.

Calculations to Print Even Numbers up to N

Calculation 1 – Increment in Strides of 2

Following is a calculation utilizing which we can print even numbers until n by increasing the circle variable with 2 during every cycle.

1. Start.
1. Take an incentive for n. This is our furthest breaking point for the even numbers we print to support.
2. Initialize variable even with 2. Indeed, even numbers start from 2.
3. Check to assume that even is not exactly equivalent to n.
4. If the condition is valid, go to stage 6, else go to stage 9.
5. Print even.
6. Increment even by 2. Thus, that even has the following much number in the following emphasis.
7. Go to stage 4.
8. Stop.

Calculation 2 – Check Assuming Number is Even

Following is an elective method for printing even numbers up to n by checking during every emphasis assuming the number is odd.

1. Start.
2. Take an incentive for n. This is our furthest cut-off for the even numbers we print to comfort.
3. Initialize variable I with 1.
4. Check to assume I am not exactly equivalent to n.
5. If the above condition is valid, go to stage 6, else go to stage 11.
6. Check to assume that I am precisely separable by 2.
7. If the above condition is valid, go to stage 8, else go to stage 9.
8. Print I.
9. Increment I by 1.
10. Go to stage 4.
11. Stop.

We will go through every one of these calculations in our models, utilizing different circling components.

Model 1 – Display Even Numbers up to N – While Loop

In this model, we will print every one of them even numbers not exactly or equivalent to 20 utilizing Java While Loop. We will compose the Java program given the above calculations.

Java Program – Using Algorithm 1

/**

* Java Program – Display Even Numbers

*/

public class DisplayEvenNumbers {

public static void main(String[] args) {

//number

int n = 20;

//print all even numbers <=n

int even=2;

while (even<=n) {

System.out.print(even+” “);

indeed, even += 2;

}

}

}

Java Program – Using Algorithm 2

/**

* Java Program – Display Even Numbers

*/

public class DisplayEvenNumbers {

public static void main(String[] args) {

//number

int n = 20;

//print all even numbers <=n

int i=1;

while (i<=n) {

//check assuming that I is precisely separable by 2

if (i%2==0) {

System.out.print(i+” “);

}

i++;

}

}

}

Result

Run any of the above programs, and we will get every one of them even numbers up to n, printed to the control centre.

2 4 6 8 10 12 14 16 18 20