/* Program for Java arithmetic and type conversions */ public class arithmetic { public static void main(String[] args) { int num1, num2 = 6; float real1 = 12.45f; double bigreal, d; num1 = 10; num1 = num2 + 23 - num1 * 3; // -1 num1 = 10; num1 = num2 + (23 - num1) * 3; // 45 d = real1; // implicit conversion bigreal = -234; // implicit conversion num2 = '*'; // implicit conversion //NOT: num1 = 4.7; num1 = (int) 4.7; // explicit conversion, typecast // num1 is now 4 (truncates) num2 = (int) Math.round(4.302940); // 4 num1 = (int) Math.floor(2345.86759); // 2345 System.out.println("num1 is " + num1); num2 = (int) Math.ceil(4.3540495); // 5 System.out.println("sqrt of 26 is " + Math.sqrt(26)); System.out.println(num1); System.out.println(num1 + num2); System.out.println(num1 + ' ' + num2); System.out.println(num1 + " " + num2); System.out.println("num1 and num2 are " + num1 + num2); System.out.println(num1 + num2 + "are nums "); System.out.println("sum is " + (num1 + num2)); num1 = 3; bigreal = 20.34; d = bigreal / 2 + 14 * num1 / 5; // 18.17 d = bigreal / 2 + 14 * num1 / 5.0; // 18.57 num2 = 5; d = bigreal / 2 + 14 * (double) num1 / num2; // 18.57 num2 = 42 % 5; // 2 is 42 "mod" 5, division remainder num1 = 7 % 23; // 7 bigreal = 10 / 3; // 3.0 bigreal = 10 / 3.0; // 3.333333333333333 System.out.println(d + " " + num2 + " " + bigreal); System.out.printf("%.1f %5d %15.8f\n", d, num2, bigreal); System.out.println("end"); // shortcuts int num = 6; double result; num = num * 2; // 12 num *= 2; // 24 num = num + 10; // 34 num += 10; // 44 num = num + 1; // 45 num += 1; // 46 num++; // 47 ++num; // 48 num = 6; result = 10 + num++; System.out.println(result + " " + num); // 16.0 7 num = 6; result = 10 + ++num; System.out.println(result + " " + num); // 17.0 7 num--; int a = 9; num = a % 2 - 2 % a ; System.out.println(num); } }