I am a newbie here. LOLI am a newbie here. LOL
I would like to ask why the result comes:
"The area of circle is 314.1500000000000 here. LOLI 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 12:06 PM. 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.
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..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 + ".");
}
}