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

PHP programming language Circle Area, Circumference and ratio:  For a circle with a given radius, write a...
PHP programming language Circle Area, Circumference and ratio:  For a circle with a given radius, write a program to compute 1) area of the circle, 2) circumference of the circle and 3) the ratio of the circumference to its diameter.  You will use 3 different values of radius a) 10cm, b) 50 cm, and c) 100cm for your computations. The output should print or echo the following statements identifying the writer of the program and the computation for each value of the...
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 Python program that computes the income tax for an individual. The program should ask...
Write a Python program that computes the income tax for an individual. The program should ask the user to enter the total taxable income for the year. The program then uses the tax bracket (as shown below) to calculate the tax amount. This is based on a progressive income tax system which is how income tax is calculated in the U.S. As a result, this for example means that only income above $500,001 is taxed at 37%. Income of lower...
Write a Python program that computes the income tax for an individual. The program should ask...
Write a Python program that computes the income tax for an individual. The program should ask the user to enter the total taxable income for the year. The program then uses the tax bracket (as shown below) to calculate the tax amount. This is based on a progressive income tax system which is how income tax is calculated in the U.S. As a result, this for example means that only income above $500,001 is taxed at 37%. Income of lower...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT