Sphere, float Learn programming Java



Lesson:

Basic Data Types


Exercise:

Sphere, float


Objetive:

Calculate the surface area and volume of a sphere, given its radius (surface area = 4 * pi * radius squared; volume = 4/3 * pi * radius cubed).

Note: For floating-point numbers, you should use Convert.ToSingle(...)


Code:

import java.util.*;
public class Main
{
	public static void main(String[] args)
	{
		float radius;
		float pi = 3.1415926535f;

		System.out.print("Radius? ");
		radius = Float.parseFloat(new Scanner(System.in).nextLine());

		System.out.println(4 * pi * (radius * radius));

		System.out.println(4 / 3 * pi * (radius * radius * radius));
	}
}



Juan A. Ripoll - Systems Tutorials and Programming Courses ©  All rights reserved.  Legal Conditions.