Programming Shortcut

Write, Run and execute Your programs with simple and esay way by Programming Shortcut

Monday, February 4, 2019

Even and odd numbers list betwwen 1 to n in java

Hello friends! Welcome to my website

This session shows how to print all even and odd numbers list between 1 to n by oops concepts in java
This java language programming is free all type of errors like the compiler and runtime errors and this programming is tested to compile and then run the program code.
Here is only programming code with the output. This programming output is customized to understand easily.


Print all even numbers between 1 to n in Java 


import java.util.Scanner;
class test1 {
int n;
public void printeven(int n) {
int i =0;
while ( i< n) {
if(i%2==0){
System.out.print(i+" ");
}
i++;
}
}
}
public class evenlist {
public static void main(String[] args) {
test1 o = new test1();
Scanner c= new Scanner(System.in);
System.out.print("Enter a Number till want list:");
o.n=c.nextInt();
System.out.println("All Even Number list");
o.printeven(o.n);
}
}


Output:    Enter a Number till want list: 20
All Even Number list
0 2 4 6 8 10 12 14 16 18



Print all odd numbers between 1 to n in Java 


import java.util.Scanner;
class test1 {
int n;
public void printSum(int n) {
int i = 0;
while (i < n) {
if (i % 2 == 1) {
System.out.print(i + " ");
}
i++;
}
}
}
public class even_and_odd {
public static void main(String[] args) {
test1 o = new test1();
Scanner c = new Scanner(System.in);
System.out.print("Enter a Number till want list: ");
o.n = c.nextInt();
System.out.println("All Odd Number list");
o.printSum(o.n);
}
}


Output:  Enter a Number till want list: 20
All Odd Number list
1 3 5 7 9 11 13 15 17 19

I make this programming and hope you are like the post if are you like the post you can please comment and Share the post to reach more people.
If any doubt about this java programming code or input & output please comment below
If you have another question about java programming send an email by contacting us form are given on the page right side.
I am Try to Help in your Programming Language by Programming Shortcut

Please share your experience about this post,
Thank You!

About Programming Shortcut

This is a programming learning website that can help you learn and when your write your programming code. And all programming code is compiled run programming code in the following programming language C, c++, Java, php and Javascript Languages. find your programming read, understand the programming then copy and past and run your program and save your golden time to typing the program becuse you learn more in leas time. All Programming is codeing is ease to understand the programming