I am a newbie here. LOL
I would like to ask why the result comes:
"The area of circle is 314.15000000000003." ???
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
I am a newbie here. LOL
I would like to ask why the result comes:
"The area of circle is 314.15000000000003." ???
Last edited by copeg; September 18th, 2010 at 11:06 AM. Reason: Please use the code tags
What output would you expect?
This is a normal thing. Your computer can only be so accurate. It is not a program problem, but rather that you computer has calculated it as best as possible. I don't really know how to explain it.
cppcppcpp (September 20th, 2010)
Now go through the page I linked and correct your expectations.
db
if you want to your answer to be displayed 314.15, you should put, "%2f, area" if im not mistaken its look like in the C programming it will just put 2 decimal place, i hope it can helps you.
instead of this System.out.println("The area of circle is " + area + "."); use this System.out.printf("The area of cirlcis %3f" , area); so that the modulo will be read in the java compiler, with no problem
@sanz:
you're right..it's like C progmramming...
@cppcppcppcpp
change the System.out.println("The area of circle is " + area + ".");
to:
System.out.printf("The area of cirle is %.2f" , area);
Should be getting 314.15 for the area... I got it..
Now get more fun with it and require input from user.
import java.util.*;
public class Area {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double area;
System.out.println("Please enter a radius");
double radius = input.nextDouble();
area = radius * radius * 3.1415 ;
System.out.println("The area of circle is " + area + ".");
}
}