?
Solved

Java: Output 10%3...

Posted on 2007-10-19
8
Medium Priority
?
190 Views
Last Modified: 2013-11-23
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
Comment
Question by:zizi21
  • 4
  • 3
8 Comments
 
LVL 86

Accepted Solution

by:
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

by:zizi21
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

by:zizi21
ID: 20107793
could she be wrong ?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

by:CEHJ
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

by:willyRD
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

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

Expert Comment

by:CEHJ
ID: 20107985
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20114117
:-)
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question