Question

Write a program that calculates the area and circumference of a circle. It should ask the...

Write a program that calculates the area and circumference of a circle. It should ask the user first to enter the number of circles n, then reads the radius of each circle and stores it in an array. The program then finds the area and the circumference, as in the following equations, and prints them in a tabular format as shown in the sample output. Area = πr2 , Circumference = 2πr, π = 3.14159

Sample Output:
Enter the number of circles: 4
Enter the radius of circle 1 : 10.5
Enter the radius of circle 2 : 5
Enter the radius of circle 3 : 30.7
Enter the radius of circle 4 : 12.5

Circle Radius Area    Circumference
1 10.5 346.36 65.97
2 5   78.54 31.42
3 30.7   2960.92   192.89
4 12.5 490.87 78.54

((in java ))

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program code to copy:-

import java.util.Scanner;

import java.text.DecimalFormat;

public class Circle {

      /**

      * This program calculates the area and circumference of a circle.

      * Program asks the user first to enter the number of circles n, then

      * reads the radius of each circle and stores it in an array.

      * Program then finds the area and the circumference and

      * prints them in a tabular format.

      */

      public static void main(String[] args) {

            //Creating object of scanner class for user input

            Scanner in = new Scanner(System.in);

            //Creating object of DecimalFormat class to print the result upto 2 decimal places

            DecimalFormat df = new DecimalFormat("0.00");

            //Asking the number of circles from user

            System.out.print("Enter the number of circles: ");

            int num = in.nextInt();

           

            //Allocating the memory for array to store the radius of circle

            double radius[] = new double[num];

            //Allocating the memory for array to store the calculated area of circle

            double area[] = new double[num];

            //Allocating the memory for array to store the calculated circumference of circle

            double circumference[] = new double[num];

           

           

            for(int i=0; i<num; i++)

            {     //Reading the radius of each circle from user

                  System.out.print("Enter the radius of circle "+(i+1)+": ");

                  radius[i] = in.nextDouble();

                 

                  //Calculating the area of each circle

                  area[i] = 3.14159 * radius[i] * radius[i];

                 

                  //Calculating the circumference of each circle

                  circumference[i] = 2 * 3.14159 * radius[i];

            }

           

            //Printing the result in tabular format

           

            //Printing heading

            System.out.println("Circle\tRadius\tArea\tCircumference");

            //Printing the area and circumference of circle for each radius given

            for(int i=0; i<num; i++)

            {

                  System.out.println((i+1)+"\t"+radius[i]+"\t"+df.format(area[i])+"\t"+df.format(circumference[i]));

            }

           

      }

}

Sample output:-

Add a comment
Know the answer?
Add Answer to:
Write a program that calculates the area and circumference of a circle. It should ask the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT