posted 10 months ago

hello everybody i am a beginner in java i wrote a simple program of addition, subtraction, multiplication and division.

i declared the c value as double as its value is in division also. but am getting only decimal value after point that is 0.0. what should i do to get atleast 3 to 4 values after decimal.

output is

c=0.0

- 1

hello everybody i am a beginner in java i wrote a simple program of addition, subtraction, multiplication and division.

i declared the c value as double as its value is in division also. but am getting only decimal value after point that is 0.0. what should i do to get atleast 3 to 4 values after decimal.

output is

c=0.0

posted 10 months ago

- 1

When you divide an int by an int the divide operation does not result in any decimal values. The result is an int. This is especially noticeable when you have A/B and the value of B is greater than the value of A, the result will be zero.

To get a floating point result one or both operands must be float. E.g.

To get a floating point result one or both operands must be float. E.g.

Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.

posted 10 months ago

- 1

since a and b are both integer types, "a/b" does integer division. Then that is cast up to a float for the assignment.

You need to cast either a or b (usually you'd see it on the first one) to a float before your division...something like

float c = (float)a/b;

that may not be 100% correct...but it's the basic idea.

You need to cast either a or b (usually you'd see it on the first one) to a float before your division...something like

float c = (float)a/b;

that may not be 100% correct...but it's the basic idea.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors