// Question 1.
// Write a program that reads in the length of sides
// of an equilateral triangle and computes the area and volume.
import static java.lang.Math.sqrt;
import java.util.Scanner;
public class Lab_3 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
//input step.
System.out.println("Please Enter the lenght of the sides: ");
double lengthSide = scanner.nextDouble();
System.out.println("Please Enter the height of the triangle: ");
double height = scanner.nextDouble();
//procesing step.
double areaOfTriangle = sqrt(3)/4 * (Math.pow(lengthSide,2));
//heighL*height = Math.pow(height,2)
double volumeOfTriangle = areaOfTriangle * height;
//telling user what happen.
//you can use (format function) if you want after point (two,three or one number digits). like first one.
System.out.format("The area of Triangle is :%.03f ",areaOfTriangle);
System.out.println("The area of Triangle is: "+areaOfTriangle);
System.out.println("The volume of Triangle is: "+volumeOfTriangle);
}
}
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter