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
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);
}
}
All Even Number list
0 2 4 6 8 10 12 14 16 18
Print all odd numbers between 1 to n in Java
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);
}
}
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!