Java || Simple Math Using Int & Double
This page will display the use of int and double data types.
==== ADDING TWO NUMBERS TOGETHER ====
To add two numbers together, you will have to first declare your variables by doing something like this.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
// ============================================================================ // Author: Kenneth Perkins // Date: Mar 4, 2012 // Updated: Mar 7, 2021 // Taken From: http://programmingnotes.org/ // File: Add.java // Description: Demonstrates adding numbers together // ============================================================================ import java.util.Scanner; public class Add { public static void main(String[] args) { // declare variables int num1 = 0; int num2 = 0; int sum = 0; // prepare Scanner for integer data input Scanner cin = new Scanner(System.in); // get data from user System.out.print("Please enter the first number: "); num1 = cin.nextInt(); System.out.print("Please enter the second number: "); num2 = cin.nextInt(); // calculate the sum of the two numbers sum = num1 + num2; // display results to the user System.out.println("The sum of " + num1 + " and " + num2 + " is: " + sum); }// end main }// http://programmingnotes.org/ |
Notice in lines 14-16, I declared my variables, giving them a name. You can name your variables anything you want, with a rule of thumb as naming them something meaningful to your code (i.e avoid giving your variables arbitrary names like “x” or “y”). In line 29 the actual math process is taking place, storing the sum of “num1” and “num2” in a variable called “sum.” I also initialized my variables to zero. You should always initialize your variables.
I obtained data from the user by using the Scanner Class.
===== HOW TO COMPILE CODE USING THE TERMINAL =====
*** This can be achieved by typing the following command:
(Notice the .java source file is named exactly the same as the class header)
javac Add.java
*** To run the compiled program, simply type this command:
java Add
The above code should give you the following output:
Please enter the first number: 8
Please enter the second number: 24
The sum of 8 and 24 is: 32
==== SUBTRACTING TWO NUMBERS ====
Subtracting two ints works the same way as the above code, and we would only need to edit the above code in one place to achieve that. In line 29, replace the addition symbol with a subtraction sign, and you should have something like this
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
// ============================================================================ // Author: Kenneth Perkins // Date: Mar 4, 2012 // Updated: Mar 7, 2021 // Taken From: http://programmingnotes.org/ // File: Subtract.java // Description: Demonstrates subtracting numbers together // ============================================================================ import java.util.Scanner; public class Subtract { public static void main(String[] args) { // declare variables int num1 = 0; double num2 = 0; double sum = 0; // prepare Scanner for integer data input Scanner cin = new Scanner(System.in); // get data from user System.out.print("Please enter the first number: "); num1 = cin.nextInt(); System.out.print("Please enter the second number: "); num2 = cin.nextDouble(); // calculate the difference of the two numbers sum = num1 - num2; // display results to the user System.out.println("The difference between " + num1 + " and " + num2 + " is: " + sum); }// end main }// http://programmingnotes.org/ |
Note: In the above example, “cin.nextDouble()” was used on line 26 in place of “nextInt.” The declaration “nextDouble” can be used in place of “nextInt” in case you want to obtain floating point data from the user.
===== HOW TO COMPILE CODE USING THE TERMINAL =====
*** This can be achieved by typing the following command:
(Notice the .java source file is named exactly the same as the class header)
javac Subtract.java
*** To run the compiled program, simply type this command:
java Subtract
The above code should give you the following output
Please enter the first number: 8
Please enter the second number: 23.99999
The difference between 8 and 23.99999 is: -15.99999
==== MULTIPLYING TWO NUMBERS ====
This can be achieved the same way as the 2 previous methods, simply by editing line 29, and replacing the designated math operator with the star symbol “*”.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
// ============================================================================ // Author: Kenneth Perkins // Date: Mar 4, 2012 // Updated: Mar 7, 2021 // Taken From: http://programmingnotes.org/ // File: Multiply.java // Description: Demonstrates multiplying numbers together // ============================================================================ import java.util.Scanner; public class Multiply { public static void main(String[] args) { // declare variables double num1 = 0; int num2 = 0; double sum = 0; // prepare Scanner for integer data input Scanner cin = new Scanner(System.in); // get data from user System.out.print("Please enter the first number: "); num1 = cin.nextDouble(); System.out.print("Please enter the second number: "); num2 = cin.nextInt(); // calculate the product of the two numbers sum = num1 * num2; // display results to the user System.out.println("The product of " + num1 + " and " + num2 + " is: " + sum); }// end main }// http://programmingnotes.org/ |
===== HOW TO COMPILE CODE USING THE TERMINAL =====
*** This can be achieved by typing the following command:
(Notice the .java source file is named exactly the same as the class header)
javac Multiply.java
*** To run the compiled program, simply type this command:
java Multiply
The above code should give you the following output
Please enter the first number: 7.999999
Please enter the second number: 24
The product of 7.999999 and 24 is: 191.99997
==== DIVIDING TWO NUMBERS TOGETHER ====
In division, when you divide numbers together, sometimes they end in decimals. Int data types can not store decimal data (try it yourself and see), so here is where the use of the double data type is mandatory.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
// ============================================================================ // Author: Kenneth Perkins // Date: Mar 4, 2012 // Updated: Mar 7, 2021 // Taken From: http://programmingnotes.org/ // File: Divide.java // Description: Demonstrates dividing numbers together // ============================================================================ import java.util.Scanner; public class Divide { public static void main(String[] args) { // declare variables double num1 = 0; double num2 = 0; double sum = 0; // prepare Scanner for integer data input Scanner cin = new Scanner(System.in); // get data from user System.out.print("Please enter the first number: "); num1 = cin.nextDouble(); System.out.print("Please enter the second number: "); num2 = cin.nextDouble(); // calculate the quotient of the two numbers sum = num1 / num2; // display results to the user System.out.println("The quotient of " + num1 + " and " + num2 + " is: " + sum); }// end main }// http://programmingnotes.org/ |
===== HOW TO COMPILE CODE USING THE TERMINAL =====
*** This can be achieved by typing the following command:
(Notice the .java source file is named exactly the same as the class header)
javac Divide.java
*** To run the compiled program, simply type this command:
java Divide
The above code should give the following output
Please enter the first number: 7.99999
Please enter the second number: 23.99999
The quotient of 7.99999 and 23.99999 is: 0.33333305
==== MODULUS ====
If you wanted to capture the remainder of the quotient you calculated from the above code, you would use the modulus operator (%).
From the above code, you would only need to edit line 29, from division, to modulus.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
// ============================================================================ // Author: Kenneth Perkins // Date: Mar 4, 2012 // Updated: Mar 7, 2021 // Taken From: http://programmingnotes.org/ // File: Modulus.java // Description: Demonstrates performing modulus on numbers // ============================================================================ import java.util.Scanner; public class Modulus { public static void main(String[] args) { // declare variables double num1 = 0; int num2 = 0; double remainder = 0; // prepare Scanner for integer data input Scanner cin = new Scanner(System.in); // get data from user System.out.print("Please enter the first number: "); num1 = cin.nextDouble(); System.out.print("Please enter the second number: "); num2 = cin.nextInt(); // calculate the remainder of the two numbers remainder = num1 % num2; // display results to the user System.out.println("The remainder of " + num1 + " and " + num2 + " is: " + remainder); }// end main }// http://programmingnotes.org/ |
===== HOW TO COMPILE CODE USING THE TERMINAL =====
*** This can be achieved by typing the following command:
(Notice the .java source file is named exactly the same as the class header)
javac Modulus.java
*** To run the compiled program, simply type this command:
java Modulus
The above code should give the following output
Please enter the first number: 23.99999
Please enter the second number: 8
The remainder of 23.99999 and 8 is: 7.9999905
Leave a Reply