## AndroidBerry.com |

This topic demonstrates about how to programatically find out the area of basic shapes using java program.

**Steps**

- Accept the radius from user
- Initialize the value of pi = 3.1415 in float form
- Use above mentioned formula in the chart to calculate area of circle

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); } }

Enter radius : 5

Area of circle with radius 5 is 78.5375

Area of circle with radius 5 is 78.5375

**Note : **You can also use the PI constant from Math class as

float PI = (float) Math.PI; // typecast required because it is double constant

- Accept the length and breadth (width) from user
- Use above mentioned formula in the chart to calculate area of Rectange
- Display the formula result to user

**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); } }

Enter Length and Breadth : 6

12

Area of Rectangle is : 72

12

Area of Rectangle is : 72