Java Math.PI Circle Radius

Mountain State University Java Assisgnment

2.28 (Diameter, Circumference, and Area of a Circle

Write an application that inputs from the user the radius of a circle as an integer and prints the circle’s diameter, circumference, and area using the floating-point 3.14159 for PI.

Java circlePi.java Source Code

import java.util.Scanner;

public class circlePi {

public static void main( String[] args )
{
Scanner input = new Scanner( System.in );

int cRadius; //radius of circle

System.out.print( "Enter the radius of a circle " ); // prompt for user input
cRadius = input.nextInt();

//Math.PI example System.out.println("Pi is " + Math.PI);

// diameter = 'cRadius * cRadius
// circumference = 'Math.PI * 2 * cRadius'
// area = cRadius * cRadius * Math.PI'
System.out.printf( "The diameter is %d\n", cRadius * cRadius ); // prints
results for int
System.out.printf( "The circumference is %f\n", 2 * Math.PI * cRadius ); //
prints results for float
System.out.printf( "The area is %f\n", Math.PI * cRadius *cRadius ); // prints
results for float

} // end method main

} //end class circlePi

0
  Related Posts