Java || Simple Math Using Int & Float

Print Friendly, PDF & Email

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.


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


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 “*.”

===== 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).


===== 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.


===== 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