Home » Java programs » Java basic programs

Java program to find perimeter of a rectangle

In this java program, we are going to find perimeter of a rectangle, to find perimeter we need to take length and width as an input.
Submitted by IncludeHelp, on December 30, 2017

Given length and width of a rectangle and we have to find its perimeter using java program.

Formula to find perimeter of a rectangle: perimeter = 2 (Length + Width)

Example:

    Input:
    Length: 25
    Width: 22

    Output:
    Perimeter = 2 (25+22) = 94

Program to find perimeter of rectangle in java

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






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.