import java.io.*;
import java.util.*;
public class First_Even_Odd{
Scanner scan=new Scanner(System.in);
int i, starts, ends;
String choice;
public void input(){
try{
System.out.println("Enter \"Even\" to print even numbers between beg - last or \"Odd\" for odd numbers.");
choice=scan.next();
}
catch(Exception e){
System.out.println("Error Code: "+e);
System.exit(0);
}
try{
System.out.println("Enter starting number:- ");
starts=scan.nextInt();
System.out.println("Enter ending number:- ");
ends=scan.nextInt();
}
catch(NumberFormatException e){
System.out.println("Error Code: "+e);
System.exit(0);
}
}
public void compute(){
if(choice.equalsIgnoreCase("even"))
even();
else if(choice.equalsIgnoreCase("odd"))
odd();
else
exit();
}
public void even(){
System.out.println("\nEven numbers between "+starts+" and "+ends+" are:- ");
for(i=starts; i<=ends; i++)
if(i%2==0)
System.out.println(i);
}
public void odd(){
System.out.println("\nOdd numbers between "+starts+" and "+ends+" are:- ");
for(i=starts; i<=ends; i++)
if(i%2!=0)
System.out.println(i);
}
public void exit(){
System.out.println("Wrong Choice entered by you i.e. \""+choice+"\"");
}
public static void main(String[] args){
First_Even_Odd obj=new First_Even_Odd();
obj.input();
obj.compute();
}
}
import java.util.*;
public class First_Even_Odd{
Scanner scan=new Scanner(System.in);
int i, starts, ends;
String choice;
public void input(){
try{
System.out.println("Enter \"Even\" to print even numbers between beg - last or \"Odd\" for odd numbers.");
choice=scan.next();
}
catch(Exception e){
System.out.println("Error Code: "+e);
System.exit(0);
}
try{
System.out.println("Enter starting number:- ");
starts=scan.nextInt();
System.out.println("Enter ending number:- ");
ends=scan.nextInt();
}
catch(NumberFormatException e){
System.out.println("Error Code: "+e);
System.exit(0);
}
}
public void compute(){
if(choice.equalsIgnoreCase("even"))
even();
else if(choice.equalsIgnoreCase("odd"))
odd();
else
exit();
}
public void even(){
System.out.println("\nEven numbers between "+starts+" and "+ends+" are:- ");
for(i=starts; i<=ends; i++)
if(i%2==0)
System.out.println(i);
}
public void odd(){
System.out.println("\nOdd numbers between "+starts+" and "+ends+" are:- ");
for(i=starts; i<=ends; i++)
if(i%2!=0)
System.out.println(i);
}
public void exit(){
System.out.println("Wrong Choice entered by you i.e. \""+choice+"\"");
}
public static void main(String[] args){
First_Even_Odd obj=new First_Even_Odd();
obj.input();
obj.compute();
}
}
Comments