Monday, August 5, 2019

HOW TO WRITE A CODE TO PRINT ONLY EVEN NUMBER IN JAVA

Java is a general purpose programming language. It is one of the best programming language used from the day it was founded till date. Java is not just a programming language, it is the most intellectual language for beginner, professional and also developers in the aspect of software. 

We're going to discuss on how to write a simple code to display only even numbers. Now let's get started.

We're are using a "for loop"  it allows you to efficiently program a loop that needs to execute a specific number of times.

Now type:
public class evenNo {
public static void main(String[] args) {

A  for loop should have 3 parts
  • Initialization: e.g int x = 1;
  • Condition: e.g x <= 5;
  • increment/ decrements: e.g x++ or x--
Now let's apply this to our program

for (int x = 0; x <= 20; x = x+2) {
System.out.println(x);

N/B: The program should look like this



The output or result will be:
0
2
4
6
8
10 till 20

No comments:

Post a Comment