Thursday, June 19, 2014

Elementary Programming; Exercise No. 2

Write a program that reads in the radius and length of a cylinder and computes the area and volume using the following formulas:
   area = radius * radius * p
   volume = area * length

Solution:

public static void main(String[] args) {
   Scanner s = new Scanner(System.in);
   double radius, length, area, volume;

   System.out.print("Enter the radius of the cylinder:");
   radius = s.nextDouble();

   System.out.print("Enter the length of the cylinder:");
   length = s.nextDouble();

   area = radius * radius * Math.PI;
   volume = area * length;

   System.out.print("The area of the cylinder is  " + area + " and the volume of the cylinder is " + volume);
}

No comments:

Post a Comment

Author