Solved

# Java: Output 10%3...

Posted on 2007-10-19
Medium Priority
190 Views
Hi,

I have 2 questions like this:

System.out.println(10.0/3.0+5.0*2.0) where the answer is 13.33333......4

My question is how long would 33333333 be  and why? coz i printed out and it was pretty long without truncating..

My second question is that :

System.out.println(10%3+5%2) where the answer printed was 2.0

I thought it should be 2 instead of 2.0 as both are integers and there is no floats or doubles that allowed it to be promoted.

Pls clarify. thanks
0
Question by:zizi21
• 4
• 3

LVL 86

Accepted Solution

CEHJ earned 2000 total points
ID: 20107782
>>My question is how long would 33333333 be  and why? coz i printed out and it was pretty long without

That would be as long as the precision of double allows

>>System.out.println(10%3+5%2) where the answer printed was 2.0

That can't be right. It would print 2
0

Author Comment

ID: 20107790
this is the answer given by my teacher and it says 2.0

System.out.println(10 % 3 + 5 % 2);     .................     2.0
0

Author Comment

ID: 20107793
could she be wrong ?
0

LVL 86

Expert Comment

ID: 20107804
>>could she be wrong ?

Could be a misprint. Any of those numbers written as a decimal would produce 2.0 for instance
0

LVL 3

Expert Comment

ID: 20107815
Well, if you execute this testing code you'll see by yourself that System.out.println(10 % 3 + 5 % 2); prints 2, while System.out.println(10.0 % 3.0 + 5.0 % 2.0); prints 2.0. That's because the first numbers are integers, so the result is an integer, while the second option has double, so the result is a double

package test;

public class Test {
public static void main(String[] args) {
System.out.println(10 % 3 + 5 % 2);
System.out.println(10.0 % 3.0 + 5.0 % 2.0);
}
}
0

Author Comment

ID: 20107816
thank you..btw, what is the precision for double ?
0

LVL 86

Expert Comment

ID: 20107985
0

LVL 86

Expert Comment

ID: 20114117
:-)
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Javaâ€¦
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used byâ€¦
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Downâ€¦
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
###### Suggested Courses
Course of the Month9 days, 20 hours left to enroll