by Dinesh Thakur Category: array

Here is the Java Example for Even-Odd pairs within that number:

import java.util.Scanner;

public class EvenOdd
{
                      public static void main(String a[])
              {
                      Scanner sl=new Scanner(System.in);
                      System.out.print("Enter Number: ");
                      int n = sl.nextInt();
                      int odd[]= new int[2];
                      int eve[] = new int[2];
                      int o =-1, e =-1;
                      StringBuffer sb = new StringBuffer();
                      for(int i=1;i<=n;i++)
                          {
                                   if(i%2==0)
                                     {
                                     e++;
                                     eve[e]=i;
                                       }
                                     if(e==1)
                                       {
                                          sb.append(eve[0] + " " + eve[1] +" ");
                                          e = -1;
                                        }
                                      if(i%2 !=0)
                                        {
                                             o++;
                                             odd[o]=i;
                                         }
                                       if(o==1)
                                        {
                                            sb.append(odd[0]+ " " + odd[1]+",");
                                            o = -1;
                                         }
                              }
                                  System.out.println(sb.delete(sb.length()-2, sb.length()));
                  }
}

Even-Odd pairs within that number



About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.



Related Articles