Java Program to Find Perimeter of a Rectangle

You are currently viewing Java Program to Find Perimeter of a Rectangle

Java program to find perimeter of a rectangle

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class CalPerimeterOfRectangle 
{
	public static void main(String[] args)
	{     
		int width = 0;
		int length = 0;
		try
		{
			// create object of the buffer class.
			BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

			// enter length and width of the rectangle.
			System.out.print("Enter length of the rectangle : ");
			length = Integer.parseInt(br.readLine());

			System.out.print("Enter width of the rectangle : ");
			width = Integer.parseInt(br.readLine());       
		}
		// check for invalid value.
		catch(NumberFormatException ne)
		{
			System.out.print("Invalid value" + ne);
			System.exit(0);
		}
		catch(IOException ioe)
		{
			System.out.println("IO Error :" + ioe);
			System.exit(0);
		}
		
		// formula to calculate parimeter.
		int perimeter = 2 * (length + width);
		System.out.print("Perimeter of a rectangle is : " + perimeter);
	}
}

Output

Enter length of the rectangle : 25
Enter width of the rectangle : 22
Perimeter of a rectangle is : 94

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply