digrach

Convert negative number to positive number

Mar 8th, 2014
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.52 KB | None | 0 0
  1. public class ConvertNegativeToPositive {
  2.    
  3.     // Uses Math.abs to demonstrate converting a negative number
  4.     // to a positive number.
  5.  
  6.     public static void main(String[] args) {
  7.        
  8.         int total = 2 + 2 + (-2);
  9.         System.out.println("Total : " + total);
  10.        
  11.         int total2 = 2 + 2 + Math.abs(-2);
  12.         System.out.println("Total 2 : " + total2);
  13.  
  14.         System.out.println("Positive numbers are returned unchanged : " + Math.abs(10));
  15.         System.out.println("Negative numbers are returned as positive : " + Math.abs(-10));
  16.  
  17.     }
  18.  
  19. }
Advertisement
Add Comment
Please, Sign In to add comment