Saturday, October 23, 2021

Convert word problem into Java code

 In this task, you are being asked to write a given word problem as a program.

Write a program that prompts the user for a radius and then prints:

  •  The area and circumference of a circle with that radius
  •  The volume and surface area of a sphere with that radius

HINT: Area = π×radius², Circumference=2×π×radius,
Volume of sphere = (4/3)×π×radius3, Surface area = 4×π×radius2.

NOTE: Use Math class constants and functions in your program.

Code:

import java.util.Scanner;

public class lab4Task5{

public static void main(String[] args) {

Scanner Ahsan = new Scanner(System.in);

double area, CircumFarance, volum, Surfac, radius; 

System.out.print("Enter the Radius: "); 

radius = Ahsan.nextDouble();


area = Math.PI * Math.pow(radius, 2);

CircumFarance = 2 * Math.PI * radius;

volum =(int) (4 / 3) * Math.PI * Math.pow(radius, 3);

Surfac = 4 * Math.PI * Math.pow(radius, 3);

System.out.println("Area of the Circle is:                " + area);

System.out.println("The CircumFarance of the Circle is:   " + CircumFarance);

System.out.println("The Volume of the Circle is:          "+ volum);

System.out.println("The SurfacArea of the Circle is:      " + Surfac);

}

}

OutPut:

Convert word problem into Java code


No comments:

Post a Comment

String Array and passing method in java

 In this task, we pass a method and also cover the passing String Array to the method. Code:   import java.util.Scanner; public class String...