+ 2

Can anyone help me with this Java coding?

Write a java program that print the circumference of a circle. The input of the program is diameter. Given that the diameter is 11.8. Display the result in three decimal places. (Note π = Math.PI)

25th Oct 2020, 8:57 AM
Leong Jing Wei
Leong Jing Wei - avatar
4 Antworten
+ 5
Try to use format.. System.out.format ( "%.3f", c); This will give you 3 decimal places. Also nextInt() is used for taking integer inputs. for double, you should use nextDouble() method.
25th Oct 2020, 9:41 AM
Minho
Minho - avatar
+ 4
Circumference of a circle = π * D ( D = diameter) and for three decimal places you can use format.
25th Oct 2020, 9:16 AM
Minho
Minho - avatar
+ 2
Below is my attempt but I don’t know how to display in 3 decimal places. Scanner keyboard = new Scanner (System.in); double d, c; System.out.print("The diameter of circle = "); d = keyboard.nextInt(); double r = d/2; c = (Math.PI)*r*(2/1.0); System.out.println(c);
25th Oct 2020, 9:38 AM
Leong Jing Wei
Leong Jing Wei - avatar
+ 2
Alright, thank you! I just solved it!
25th Oct 2020, 9:58 AM
Leong Jing Wei
Leong Jing Wei - avatar