Pages

Saturday 24 December 2016

Problem 61 - Find absolute value

    Find absolute value of float, int, double and long adalah suatu cara agar semua nilai dalam variabel tersebut menjadi bernilai positif


DATA HOSTED WITH ♥ BY PASTEBIN.COM - DOWNLOAD RAW - SEE ORIGINAL
  1. public class FindAbsoluteValueExample {
  2.  
  3.   public static void main(String[] args) {
  4.    
  5.     int i = 8;
  6.     int j = -5;
  7.    
  8.     /*
  9.      * To find absolute value of int, use
  10.      * static int abs(int i) method.
  11.      *
  12.      * It returns the same value if the agrument is non negative value, otherwise
  13.      * negation of the negative value.
  14.      *
  15.      */
  16.    
  17.      System.out.println("Absolute value of " + i + " is :" + Math.abs(i));
  18.      System.out.println("Absolute value of " + j + " is :" + Math.abs(j));
  19.    
  20.      float f1 = 10.40f;
  21.      float f2 = -50.28f;
  22.  
  23.     /*
  24.      * To find absolute value of float, use
  25.      * static float abs(float f) method.
  26.      *
  27.      * It returns the same value if the agrument is non negative value, otherwise
  28.      * negation of the negative value.
  29.      *
  30.      */
  31.     System.out.println("Absolute value of " + f1 + " is :" + Math.abs(f1));
  32.     System.out.println("Absolute value of " + f2 + " is :" + Math.abs(f2));
  33.    
  34.     double d1 = 43.324;
  35.     double d2 = -349.324;
  36.     /*
  37.      * To find absolute value of double, use
  38.      * static double abs(double d) method.
  39.      *
  40.      * It returns the same value if the agrument is non negative value, otherwise
  41.      * negation of the negative value.
  42.      *
  43.      */
  44.     System.out.println("Absolute value of " + d1 + " is :" + Math.abs(d1));
  45.     System.out.println("Absolute value of " + d2 + " is :" + Math.abs(d2));
  46.    
  47.     long l1 = 34;
  48.     long l2 = -439;
  49.     /*
  50.      * To find absolute value of long, use
  51.      * static long abs(long l) method.
  52.      *
  53.      * It returns the same value if the agrument is non negative value, otherwise
  54.      * negation of the negative value.
  55.      *
  56.      */
  57.     System.out.println("Absolute value of " + l1 + " is :" + Math.abs(l1));
  58.     System.out.println("Absolute value of " + l2 + " is :" + Math.abs(l2));
  59.    
  60.   }
  61. }


Dan tampilan jika di jalankan code atas akan tampak seperti gambar di bawah ini

0 comments:

Post a Comment