## Java || Simple Math Using Int & Float This page will display the use of int and float data types. Note: you can (obviously) add as many int’s together as you wish, not just two.

==== ADDING TWO NUMBERS TOGETHER ====

To add two numbers together, you will have to first declare your variables by doing something like this.

``` #1 - Adding 2 Numbers Together Java 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/ 123456789101112131415161718192021222324252627282930 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 8-10, 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 23 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 23, replace the addition symbol with a subtraction sign, and you should have something like this

``` #2 - Subtracting 2 Numbers Together Java import java.util.Scanner; public class Subtract { public static void main(String[] args) { // declare variables int num1=0; float num2=0; float 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.nextFloat(); // 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/ 123456789101112131415161718192021222324252627282930 import java.util.Scanner; public class Subtract{    public static void main(String[] args)     {        // declare variables        int num1=0;        float num2=0;        float 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.nextFloat();                // 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.nextFloat()” was used on line 20 in place of “nextInt.” The declaration “nextFloat” 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 23, and replacing the designated math operator with the star symbol “*.”

``` #3 - Multiplying 2 Numbers Together Java import java.util.Scanner; public class Multiply { public static void main(String[] args) { // declare variables float num1=0; int num2=0; float 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.nextFloat(); 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/ 123456789101112131415161718192021222324252627282930 import java.util.Scanner; public class Multiply{    public static void main(String[] args)     {        // declare variables        float num1=0;        int num2=0;        float 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.nextFloat();                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 float data type is mandatory (Note: You can also use the “double” data type here aswell).

``` #4 - Dividing 2 Numbers Together Java import java.util.Scanner; public class Divide { public static void main(String[] args) { // declare variables float num1=0; float num2=0; float 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.nextFloat(); System.out.print("Please enter the second number: "); num2 = cin.nextFloat(); // 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/ 123456789101112131415161718192021222324252627282930 import java.util.Scanner; public class Divide{    public static void main(String[] args)     {        // declare variables        float num1=0;        float num2=0;        float 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.nextFloat();                System.out.print("Please enter the second number: ");        num2 = cin.nextFloat();                // 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 23, from division, to modulus.

``` #5 - Getting the Remainder of 2 Numbers Java import java.util.Scanner; public class Modulus { public static void main(String[] args) { // declare variables float num1=0; int num2=0; float 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.nextFloat(); 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/ 123456789101112131415161718192021222324252627282930 import java.util.Scanner; public class Modulus{    public static void main(String[] args)     {        // declare variables        float num1=0;        int num2=0;        float 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.nextFloat();                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```