ARITHMETIC EXPRESSIONS




In Java programming language, there are two division operations:
/ division and % calculation of the remainder of the division.

What to remember:
1) The operation of calculating the remainder of division (%) is performed ONLY on integers
2) The result of the division operation (/) depends on the type of operands
The rule here is as follows:
When dividing an integer by an integer, the fractional part is always discarded, regardless of what type of variable we store the value!

When saving the material result into an integer variable, the fractional part will also be discarded

Let's analyze examples of performing division operations:

int i;
double x;
i = 7;
x = i / 4;            // x=1, divides the integer into the integer

x = i / 4.;           // x=1.75, divides the integer into the real (4 - without a dot is perceived as an integer, 4. (with a dot) - it's a real number!)

x =(double) i / 4;    // x=1.75, divides the real into the integer  - here the variable i is converted to a real number - this is an explicit conversion of type

Task
1) In lines 7, 9 and 11, organize the output of the variable value calculated in the previous line (organize the output from a new line).
2) Run the program
3) Make sure that the program works exactly as it is written in the theoretical part.
4) Analyze the answers
Java
1
public class Main {     
2
    public static void main(String[] args) {     
3
        int i;     
4
        double x;     
5
        i = 7;     
6
        x = i / 4;     
7
8
        x = i / 4.;     
9
10
        x =(double) i / 4;     
11
12
    }     
13
}     
Your last submission is saved in the editor window.
     

Results:

All results: