Find elements

Display even and odd numbers without if statement in Java

Display even and odd numbers without if statement in Java

In this tutorial, we will discuss the concept of Display even and odd numbers without if statement in Java language.

In this post, we are going to learn how to display even and odd numbers without if statement in Java language.

 

First, we must understand how to identify an odd and even numbers

What is Even or Odd

When the number is divided by 2 and the balance becomes zero and the above number is called as the even number – eg 2,4,6,8,10

and on the other sides when it is divided by 2 and balance becomes 1 they are called odd numbers or uneven numbers

 

In my previous post, I have explained the various approaches to print odd and even numbers using if statements in Java programming language

 

here, we will discuss how to display odd and even numbers without if statements

 

There are three programs given below

Print even and odd numbers using for loop

Program 1

The program allows the user to enter the maximum number for display odd and even numbers using for loop in Java language

Then, it will display the even and odd numbers without if statements in Java language

import java.util.Scanner;
class EvenOddDisplay{
public static void main (String args[]){
Scanner scan=new Scanner(System.in);
//create a scanner object for input

System.out.print("Enter the maximum number\n");

int num=scan.nextInt();

//print even number using for loop without if statements
System.out.print("Even numbers are:");
for(int i=0; i<=num; i+=2){
System.out.print(i+" ");

}

//print odd number using for loop without if statements
System.out.print("\n\nOdd numbers are:");
for(int i=1; i<=num; i+=2){
System.out.print(i+" ");

}
}
}

 

When the above code is executed, it produces the following results

Enter the maximum number
25
Even numbers are: 0  2  4  6  8  10  12  14  16  18  20  22  24

Odd numbers are: 1  3  5  7  9  11  13  15  17  19  21  23  25

 

Print even and odd numbers using while loop

Program 2

The program allows the user to enter the maximum number for display odd and even numbers using while loop in Java language

Then, it will display the even and odd numbers without if statements in Java language

import java.util.Scanner;
class EvenOddDisplay1{
public static void main (String args[]){
Scanner scan=new Scanner(System.in);
//create a scanner object for input

System.out.print("Enter the maximum number\n");

int num=scan.nextInt();

//print even number using for loop without if statements
System.out.print("Even numbers are:");
int i=0; 
while(i<=num){
System.out.print(i+" ");
 i+=2;
}

//print odd number using for loop without if statements
System.out.print("\n\nOdd numbers are:");
i=1; 
while(i<=num){
System.out.print(i+" ");
 i+=2;
}
}
}

When the above code is executed, it produces the following results

Enter the maximum number
30
Even numbers are: 0  2  4  6  8  10  12  14  16  18  20  22  24 26  28  30

Odd numbers are: 1  3  5  7  9  11  13  15  17  19  21  23  25  2  29

 

Print even and odd numbers using the do-while loop

Program 3

The program allows the user to enter the maximum number for print odd and even numbers using do while loop in Java language

Then, it will display the even and odd numbers without if statements in Java language

import java.util.Scanner;
class EvenOddDisplay2{
public static void main (String args[]){
Scanner scan=new Scanner(System.in);
//create a scanner object for input

System.out.print("Enter the maximum number\n");

int num=scan.nextInt();

//print even number using for loop without if statements
System.out.print("Even numbers are:");
int i=0; 
do{
System.out.print(i+" ");
 i+=2;
}while(i<=num);

//print odd number using for loop without if statements
System.out.print("\n\nOdd numbers are:");
i=1; 
do{
System.out.print(i+" ");
 i+=2;
}while(i<=num);
}
}

 

When the above code is executed, it produces the following results

Enter the maximum number
30
Even numbers are: 0  2  4  6  8  10  12  14  16  18  20  22  24 26  28  30

Odd numbers are: 1  3  5  7  9  11  13  15  17  19  21  23  25  2  29

 

Similar post

Print even and odd numbers without if statement in C++

Print even and odd numbers without if statement in C

Print  even and odd numbers without if statement in Python

C program to find the largest among three numbers using function

Python code to find the largest of three numbers using the function

C++ code to find the largest of three numbers using the function

Java code to find the largest of three numbers

C code to find the largest of three numbers

C++ code to find the largest of three numbers

Python code to find the largest of three numbers

Java program to find the middle of three number using Method

C code to find the middle of three number using the function

C++ code to find the middle of three number using the function

Python code to find the middle of three number using the function

Java code to find middle of three numbers

C code to find middle of three numbers

C++ code to find middle of three numbers

Python code to find middle of three numbers

Java code to find largest of three numbers in an array

Java code to find smallest of three numbers in an array

C code to find the middle of three number using the function

C++ code to find the middle of three number using the function

 

Suggested for you

The operator in C++ language

If statement in C++ language

Nested if statement in C++ language

Data type in C++ language

Variable in C++ language

 

Operator in C language

If statement in C language

Data type in C language

Variable in C language

 

The operator in Python language

If statement in Python language

function in Python

Data types and Variable in Python

for loop in Python language

while loop in Python language

 

Datatype and variables in Java programming language

Operator in Java language

If statements in Java language

Method in Java language

 

 

 

 

 

 

Count even and odd numbers of an array in C++
Display even and odd numbers without if statement in C
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago

Java program to check odd or even using recursion

Java program to check odd or even using recursion In this tutorial, we discuss a…

2 months ago