Search this site

Area of a Circle Calculator

The following program will prompt a user to enter a radius and then output the area of a circle with that radius.


  

    /*Area Of A Circle Calculator*/


    //import the scanner
    import java.util.Scanner;

    //import the math class
    import java.lang.Math;


    //name the class
    public class AreaOfACircle{


    //declare the main method
    public static void main(String args[]){

                 //accept input from user
                   Scanner input = new Scanner(System.in);

                  //display introduction
                   System.out.println("Welcome to the Area of a Circle Calculator.");


    //declare variables
    double radius, answer;
    double pi = Math.PI;

    //prompt user for radius
    System.out.print("\nEnter the radius of any circle below and this program will calculate             the area of that circle.\n\nRadius: ");

     //declare radius as whatever user imports
     radius = input.nextDouble();

     //calculate area of a circle
     answer = pi * (radius * radius);

     //display answer
     System.out.println("\nArea = "  + Math.round(answer));


          //create a loop to allow a user to input another radious after each answer
          while (radius != 0){

           //prompt user for another radius
           System.out.println("\nIf you would like, you may enter another radius\n(or Press 0 to                 quit)\n\nRadius: ");

     //place new input in radius variable
     radius = input.nextDouble();

     //calculate area of circle again
     answer = pi * (radius * radius);

      //display answer
      System.out.println("\nArea = " + Math.round(answer));


              //when the user types in 0 for radius
              if (radius == 0){


              //display closing message
              System.out.println("Thank you for using this program.");

              //end the loop
              break;
                      }

                  }
            }
    
    }
//end program



Comments