Thursday, February 19, 2015

Benford java in pgm



Benford.java

public class Benford
{
              public static int leadingDigit(int x)
             {
         while (x >= 10)
         {
            x = x / 10;
         }
         return x;
   }

   public static void main(String[] args)
   {
      int[] count = new int[10];    
      int N = 0;                   

      while (!StdIn.isEmpty())
      {
         int x = StdIn.readInt();      
         int digit = leadingDigit(x);  
         count[digit]++;               
         N++;
      }
  
      for (int i = 1; i < 10; i++)
         StdOut.printf("%d: %6.1f%%\n", i, 100.0 * count[i] / N);
   }
}

No comments:

Post a Comment