Calculate the Circle and Rectangle for Shapes in Java


The length & breadth of a rectangle and radius of a circle are input through the keyboard. Write a program to calculate the area & perimeter of the rectangle, and the area & circumference of the circle.

  • The program starts by importing the java.util.Scanner package, which allows the user to input data into the program.
  • Then, it declares four variables of float data type - len (length of rectangle), bre (breadth of rectangle), rad (radius of circle), and area_rect, per_rect, area_cir, circum_cir (areas and perimeters of rectangle and circle).
  • The program then prompts the user to enter the len, bre, and rad values by displaying the message "Enter the Length , Circle and Radius :". It takes the input using the Scanner object and stores it in their respective variables.
  • Next, the program calculates the area_rect and per_rect of the rectangle using the formulas len*bre and 2*(len+bre) respectively.
  • The program then calculates the area_cir and circum_cir of the circle using the formulas 3.14f*rad*rad and 2*3.14f*rad respectively.
  • Finally, the program displays the area_rect, per_rect, area_cir, and circum_cir using the System.out.println() method.

Overall, this program is a basic example of how Java can be used to perform simple mathematical operations and input/output operations, specifically for calculating the areas and perimeters of rectangles and circles.


Source Code

import java.util.Scanner;
class Calculate_Rectangle_Circle
{
	public static void main(String args[])
	{
		Scanner input = new Scanner(System.in);
		System.out.println("Enter the Length , Circle and Radius :");
		float len = input.nextInt();
		float bre = input.nextInt();
		float rad = input.nextInt();
		float area_rect = (len*bre);
		float per_rect = (2*(len+bre));
		float area_cir = (3.14f*rad*rad);
		float circum_cir = (2*3.14f*rad);
		System.out.println("Area of Rectangle : "+area_rect);
		System.out.println("Perimeter of Rectangle : "+per_rect);
		System.out.println("Area of Circule : "+area_cir);
		System.out.println("Circum of a Circule : "+circum_cir);
	}
}

Output

Enter the Length , Circle and Radius :
12
34
9
Area of Rectangle : 408.0
Perimeter of Rectangle : 92.0
Area of Circule : 254.34
Circum of a Circule : 56.52