Java code to display all even or odd number from 1 to n
- Home
- Find elements
- Java code to display all even or odd number from 1 to n
- On
- By
- 4 Comments
- Categories: Find elements
Java code to display all even or odd number from 1 to n
Java code to display all even or odd number from 1 to n
In this tutorial, we will discuss the Java code to display all even and odd number from 1 to n
In this program, we are going to learn about how to find odd or even number from 1 to given number using the loops in the Java language
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 side 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 ways to check whether the number is even or odd in Java language
Once This program received the number it will check the given number either odd or even number of given range using loops.
After receiving the input from the user, it is stored in the variable of num and then program divides the value of num by 2 and displays the output
Example for Java code to display all even or odd number from 1 to n
Print all even number using for loop
In this program, we display all even numbers from 1 to n using for loop
import java.util.Scanner; class Even_Oddtill{ public static void main (String args[]){ int i; Scanner scan=new Scanner(System.in); //create a scanner object for input System.out.print("Print all even number until:\n "); int num=scan.nextInt();//Reads input from user and stored in variable num System.out.print("Even number from 1 to "+num+" are: \n"); for(i=1; i<=num; i++){//loop for iterate from 1 to maximum if(i%2==0){//using modular operator for finding even numbers System.out.print(i+"\n"); } } } }
When the above code is executed, it produces the following results
Print all even number until: 50 Even number from 1 to 50 are: 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50
Program 2
Print all odd number using for loop
In this program, we print all odd numbers from 1 to n using for loop
import java.util.Scanner; class Even_Oddtill1{ public static void main (String args[]){ int i; Scanner scan=new Scanner(System.in); //create a scanner object for input System.out.print("Print all odd number until:\n"); int num=scan.nextInt();//Reads input from user and stored in variable num System.out.print("Odd number from 1 to "+num+" are: \n"); for(i=1; i<=num; i++){//loop for iterate from 1 to maximum if(i%2==1) System.out.print(i+"\n"); } } } }
When the above code is executed, it produces the following results
Print all odd number until: 50 Odd number from i to 50 are 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49
Program 3
In this program, we print all even numbers from 1 to n using while loop
Print all even number using while loop
import java.util.Scanner; class Even_Oddtill3{ public static void main (String args[]){ int i; Scanner scan=new Scanner(System.in); //create a scanner object for input System.out.print("Print all even number until:\n"); int num=scan.nextInt();//Reads input from user and stored in variable num System.out.print("even number from i to "+num+" are: \n"); i=1; while(i<=num){//loop for iterate from 1 to maximum if(i%2==0){ System.out.print(i+"\n"); } i++; } } }
When the above code is executed, it produces the following results
Print all even number until: 50 Even number from 1 to 50 are: 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50
Print all odd number using while loop
In this program, we print all odd numbers from 1 to n using while loop
Program 4
import java.util.Scanner; class Even_Oddtill2{ public static void main (String args[]){ int i; Scanner scan=new Scanner(System.in); //create a scanner object for input System.out.print("Print all odd number until:\n"); int num=scan.nextInt();//Reads input from user and stored in variable num System.out.print("Odd number from i to "+num+" are: \n"); i=1; while(i<=num){//loop for iterate from 1 to maximum if(i%2==1){ System.out.print(i+"\n"); } i++; } } }
When the above code is executed, it produces the following results
Print all odd number until: 50 Odd number from i to 50 are 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49
Program 5
In this program, we print all even numbers from 1 to n using the do-while loop
Print all even number using the do-while loop
import java.util.Scanner; class Even_Oddtill4{ public static void main (String args[]){ int i; Scanner scan=new Scanner(System.in); //create a scanner object for input System.out.print("Print all even number until:\n"); int num=scan.nextInt();//Reads input from user and stored in variable num System.out.print("even number from i to "+num+" are: \n"); i=1; do{ //loop for iterate from 1 to maximum if(i%2==0){ System.out.print(i+"\n"); } i++; } while(i<=num); } }
When the above code is executed, it produces the following results
Print all even number until: 50 Even number from 1 to 50 are: 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50
Program 6
Print all odd number using the do-while loop
In this program, we print all odd numbers from 1 to n using the do-while loop
import java.util.Scanner; class Even_Oddtill5{ public static void main (String args[]){ int i; Scanner scan=new Scanner(System.in); //create a scanner object for input System.out.print("Print all odd number until:\n"); int num=scan.nextInt();//Reads input from user and stored in variable num System.out.print("odd number from i to "+num+" are: \n"); i=1; do{ //loop for iterate from 1 to maximum if(i%2==1){ System.out.print(i+"\n"); } i++; } while(i<=num); } }
When the above code is executed, it produces the following results
Print all odd number until: 50 Odd number from i to 50 are 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49
Suggested for you
For loop in Java programming language
Do while loop in Java language
Similar post
C program to find a number is even or odd using the function
C program to separate Odd and Even numbers from an array
C program to Electricity bill calculation using the function
C program to display all even and odd numbers from 1 to n
C program display odd and even numbers without if statements
C program to calculate the sum of odd and even numbers
C program to find whether a number is even or odd
C++ program to find a number is even or odd using the function
C++ program to separate Odd and Even numbers from an array
C++ program to display all even and odd numbers from 1 to n
C++ program calculate Average of odd and even numbers in an array
C++ program to calculate the sum of odd and even numbers
C++ program to find whether a number is even or odd
4 Comments
Fleck 5600SXT package April 5, 2020 at 10:16 pm
This is very interesting, You’re a very skilled blogger. I have joined your feed
and look forward to seeking more of your excellent post. Also, I’ve
shared your web site in my social networks!
Karmehavannan April 6, 2020 at 6:07 am
thank you
Maisie November 20, 2020 at 10:12 pm
Awesome blog! Do you have any helpful hints for aspiring writers?
I’m hoping to start my own website soon but I’m a little
lost on everything. Would you advise starting with a free platform
like WordPress or go for a paid option? There are so many choices out there that I’m
completely overwhelmed .. Any tips? Bless you!
ganesh August 7, 2023 at 7:11 am
thank you