![]() |
![]() |
This topic demonstrates about how to programatically find out the area of basic shapes using java program.
Steps
import java.util.Scanner; public class AreaOfCircleEx { public static void main(String[] args) { int radius; float PI = 3.1415f,area; Scanner scan = new Scanner(System.in); System.out.print("Enter radius : "); radius = scan.nextInt(); area = PI * (radius * radius); // pi * r square System.err.println("Area of circle with radius "+radius+" is "+area); } }
Note : You can also use the PI constant from Math class as
float PI = (float) Math.PI; // typecast required because it is double constant
Note : Breadth value should be greater than Length value.
Program
import java.util.Scanner; public class AreaOfRectangleEx { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int length,breadth,area; System.out.print("Enter Length and Breadth : "); length = scan.nextInt(); breadth = scan.nextInt(); area = length * breadth; System.out.print("Area of Rectangle is : "+area); } }