Question

In: Computer Science

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

(( in java ))

Solutions

Expert Solution

Answer:

import java.util.Scanner; //importing Scanner class for taking the data from the keyboard
import java.text.DecimalFormat; //importing DecimalFormat class for formatting the output
public class Circle //class Circle starts here
{
private static DecimalFormat df = new DecimalFormat("#.##"); /* Object 'df' is created of class DecimalFormat and setting two digits after the decimal point */
public static void main(String[] args) // main() function starts here
{
double area,circumference; // variable declaration
   int n; // variable declaration
Scanner s = new Scanner(System.in); // 's' is the object of Scanner class
System.out.print("Enter the number of circles: "); // prompts the message
n = s.nextInt(); // reads an integer from the keyboard and stores in 'n' (no. of circles)
double a[] = new double[n]; // An array of double type is created with size 'n'
for(int i = 0; i < n ; i++) // loop repeats for 'n' times
{
System.out.printf("Enter the radius of circle "+(i+1)+": "); // prompts the message
   a[i] = s.nextDouble(); // reading a double value from the keyboard and storing in the array
}
for(int i = 0; i < n ; i++) // loop repeats for 'n' times
{
area = (3.14159*a[i]*a[i]); // calculates the area of (i+1)th circle)
   circumference = (2*3.14159*a[i]); //calculates the circumference (i+1)th circle)
   System.out.println("The area and circumference of circle "+(i+1)+" is: "+df.format(area)+", "+df.format(circumference));
   /* displaying the area, circumference of (i+1)th circle by rounding the result to 2 decimal places */
   }
} // end of main()
} // end of class

Program Screenshot:

Output:


Related Solutions

Write a program in C that computes the area of a circle (Area = pi *...
Write a program in C that computes the area of a circle (Area = pi * r2) and the volume of a sphere (Volume = 4/3 * pi * r3). Both formulas use r which is the radius. Declare a float variable pi = 3.14159. Get the value of r from the keyboard and store it in a float variable. Display both the area of the circle and the volume of the sphere.
Write a program to calculate the area and circumference of a cuboid. Use printf to display...
Write a program to calculate the area and circumference of a cuboid. Use printf to display 2 digits after decimal point in all the results Program should be in java language.
Write a C program that calculates a student grade in the C Programming Class. Ask the...
Write a C program that calculates a student grade in the C Programming Class. Ask the user to enter the grades for each one of the assignments completed in class: Quiz #1 - 25 points Quiz #2 - 50 points Quiz #3 - 30 points Project #1 - 100 points Project #2 - 100 points Final Test - 100 points The total of the quizzes count for a 30% of the total grade, the total of the projects counts for...
Write a program that calculates the salary of employees. The program should prompt the user to...
Write a program that calculates the salary of employees. The program should prompt the user to enter hourly rate and number of hours of work a day. Then, the program should display the salary daily, bi-weekly (5 days a week), and monthly. Sample program: Enter your hourly rate: >>> 20 Enter how many hours you work a day: >>> 8 Your daily salary is: $160 Your bi-weekly salary is: $1600 Your monthly: $3200
Write a program that calculates the average of a four lab marks. It should use the...
Write a program that calculates the average of a four lab marks. It should use the following functions: • void getMarks() should ask the user for a lab marks, store it in a reference parameter variable, and validate it so that lab marks lower than 0 or higher than 100 is not accepted. This function should be called by main once for each of the four lab marks to be entered. • void avgGradeDisp() should calculate and display the average...
Write a program to calculate the area of four shapes (Rectangle, triangle, circle and square). The...
Write a program to calculate the area of four shapes (Rectangle, triangle, circle and square). The program to present the user with a menu where one of the shapes can be selected. Based on the selection made, the user enters the proper input, the program validates the input (i.e all entries must be greater than zero). Once the input is entered and validated, the intended area is calculated and the entered information along with the area are displayed. Area of...
Write a program that displays a weekly payroll report. A loop in the program should ask...
Write a program that displays a weekly payroll report. A loop in the program should ask the user for the employee number, gross pay, state tax, federal tax, and FICA withholdings. The loop will terminate when 0 is entered for the employee number. After the data is entered, the program should display totals for gross pay, state tax, federal tax, FICA withholdings, and net pay. Input Validation: Do not accept negative numbers for any of the items entered. Do not...
Write a java program that contains 3 overloaded static methods for calculating area of a circle,...
Write a java program that contains 3 overloaded static methods for calculating area of a circle, area of a cylinder and volume of a cylinder. Also create an output method which uses JOptionPaneto display instance field(s) and the result of the computing. Then code a driver class which will run and test calling each of these overloaded methods with hard-coded data and display the data and the result of the calculation by calling output method. Thanks!!
what should this program do? Write a program (Lab8.cpp) that will ask the user for two...
what should this program do? Write a program (Lab8.cpp) that will ask the user for two file names. You will read the characters from each file and put them in separate queues. Then, you will read each character from each queue and compare it. If every character from both queues is the same, you will print “The files are identical.” Otherwise, you will print “The files are not identical.” Step-by-Step Instructions Create a character queue (named queue1) using the Standard...
/* Writing a program that displays the following: calculating area of a circle, calculating area of...
/* Writing a program that displays the following: calculating area of a circle, calculating area of a rectangle, calculating area of a triangle, and quit. */ import java.text.DecimalFormat; import java.util.Scanner; public class GeoCalculator {    public static void main(String arg[]) { char selection; Scanner get = new Scanner(System.in); //having user input DecimalFormat twoDigits = new DecimalFormat("0.00"); //formatting area to two decimal places //display choice of selection System.out.println("Geometry Calculator"); System.out.println("Please select from the following menu:"); System.out.println("1. Calculate the Area of a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT