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

Popular Article