In: Computer Science
8. Complete the program that calculates the volume of a cube. If the side length entered for the cube is negative, the program should display an error message saying the length should be positive. If the side length entered for the cube is greater than 100, the program should print a message saying the side is too big. Otherwise the program should calculate and print the volume of the cube of the given side length.
Sample program runs are shown below: import java.util.Scanner; public class CubeVolume { public static void main(){ Scanner reader = new Scanner(System.in); System.out.print("Enter the cube side length: "); double side = reader.nextDouble();
Java code(The given code is in java):
import java.util.Scanner;
public class CubeVolume{
public static void main(String[] args){
Scanner reader=new
Scanner(System.in);
//accepting cube side
length
System.out.print("Enter
the cube side length: ");
//reading it
double
side=reader.nextDouble();
//checking if it is
negative
if(side<0)
//printing it should be positive
System.out.println("The length should be positive.");
//checking if length of
the side is 100
else
if(side>100)
//printing side is too big
System.out.println("The side is too big.");
else
//printing volume
System.out.println("The volume of cube of length "+side+" is
"+(side*side*side));
}
}
Screenshot:
Input and Output: