Complete the program to calculate and print the circumference and area of a circle, rounded to the nearest tenth (1 decimal place). The starter code already prompts the user and takes in the radius as a double-value input. You need to do the calculations and print the results. I recommend using the printf() command (described in chapter 3 of the book) to print the results with the correct rounding.
import java.util.Scanner;
class Circle {
static Scanner sc = new Scanner(System.in);
public static void main(String args[])
{
System.out.print("Enter the radius: ");
/*We are storing the entered radius in double
* because a user can enter radius in decimals
*/
double radius = sc.nextDouble();
//Area = PI*radius*radius
double area = Math.PI * (radius * radius);
System.out.println("The area of circle is: " + area);
//Circumference = 2*PI*radius
double circumference= Math.PI * 2*radius;
System.out.println( "The circumference of the circle
is:"+circumference) ;
}
}
The code keeps having outputs of decimals when I need it to have an output of powers. Do you know how I can fix that in my code to make it go to the power of a number. Here is a test case output.
sample output:
Enter the radius of a circle in centimeters:
5
Circumference = 31.4 cm
Area = 78.5 cm^2
/* Use this trick
1) Multiply the number by 10
2)Round using Math.round to the nearest integer
3)Finally divide by 10 */
import java.util.Scanner;
class Circle {
static Scanner sc = new Scanner(System.in);
public static void main(String args[])
{
System.out.print("Enter the radius: ");
/*We are storing the entered radius in double
* because a user can enter radius in decimals
*/
double radius = sc.nextDouble();
//Area = PI*radius*radius
double area = Math.round(Math.PI * (radius * radius)*10);
System.out.println("The area of circle is: " + (area/10));
//Circumference = 2*PI*radius
double circumference= Math.round(Math.PI * 2*radius*10);
System.out.println( "The circumference of the circle
is:"+(circumference/10)) ;
}
}
Get Answers For Free
Most questions answered within 1 hours.