title: To check whether the given number series is in Arithmetic progression or Geometric progression or Harmonic progression
body: <p>I am writing a program to check whether the given number series is in AP, GP or HP. I know that AP is based on common difference and GP is based on common ratio. My code works for AP and GP but fails for HP. What corrections should be made in the following code, also is there a way to optimize my code. Following is the program:</p>
  
  <pre><code>import java.util.*;
  class APGPHP 
  {
  public static void main(String[] args) 
  {
      int n;
      System.out.println("Enter the number of elements in the series");
      Scanner s = new Scanner(System.in);
      n = s.nextInt();
      int[] a = new int[n];
      System.out.println("Enter the numbers");
      for (int i = 0;i&lt;n ; i++)
      {
          System.out.println("Enter the number ["+(i+1)+"] :");
          a[i] = s.nextInt();
      }
      CheckAPGPHP(a); 
  }
  
  public static void CheckAPGPHP(int[] a)
  {
      if(a.length&lt;3)
      {
          System.out.println("Array should contain atleast 3 elements ");
          return;
      }
      if(CheckAP(a) == 1)
      {
          System.out.println("AP");
          if (CheckHP(a) == 1)
              System.out.println("HP");
      }
  
      if(CheckGP(a) == 1)
          System.out.println("GP");
  }
  
  public static int CheckAP(int[] a)
  {
      int iDiff=a[1]-a[0];
      for(int i=0;i&lt;a.length-1;i++)
      {
          if((a[i+1]-a[i])!=iDiff)
              return -1;
      }
      return 1;
  }
  
  public static int CheckGP(int[] a)
  {
      int iRatio=a[1]/a[0];
      for(int i=0;i&lt;a.length-1;i++)
      {
          if((a[i+1]/a[i])!=iRatio)
              return -1;
      }
      return 1;
  }
  
  public static int CheckHP(int[] a)
  {
      float[] b = new float[a.length];
      for (int i =0;i&lt;=b.length-1 ;i++ )
      {
          b[i] = 1.0f/a[i];
      }
      float iDiff=b[1]-b[0];
      for(int i=0;i&lt;b.length-1;i++)
      {
          if((b[i+1]-b[i])!=iDiff)
              return -1;
      }
      return 1;
  }
  }
  </code></pre>