Skip to content

1.9 Nearest Rounding of Array

Farrukh Ahmad edited this page Dec 15, 2015 · 1 revision

This for floor and ceiling of a number where floor and ceiling is given

for example,

  • Case 1: Here 5 has two 4 rounding range 4, 8. As 4 is nearest to 5 then 4 is nearest rounding of 5.

  • Case 2: [1, 2, 3, 4, 5] rounding of 2 is [2, 2, 4, 4, 6]

      public class NearestRounding {
      public static void main(String args[]){
      int a[] ={9};
      
      makeRoundngs(a, 13);
      for(int i=0; i<a.length; i++)
      {
          System.out.println(a[i]);
      }
      }
    
    
      static void makeRoundngs(int a[], int n) {
      if ( n < 0) return;
      int floorValue = 0;
      int ceilingValue = 0;
      for(int i=0; i<a.length; i++)
      {
          if(a[i] < 0)continue;
          floorValue = (a[i]/n)*n;
          ceilingValue = floorValue + n;
          
          if( a[i] - floorValue < ceilingValue - a[i])
          {
              a[i] = floorValue;
          }
          else
          {
              a[i] = ceilingValue;
          }
      }
      }
      }
    

Clone this wiki locally