Angle Bracket <> in Java with Examples Different types of Recursions in Java Java Lambda Filter Example Java Program for Shopping Bill Pythagorean Triplet with Given Sum Using Single Loop in Java TemporalAdjusters lastInMonth() method in Java with Examples ZIP API in Java Atomic reference in Java Digit Extraction in Java DRY (Don't Repeat Yourself) Principles in Java with Examples Empty array in Java Is main method compulsory in Java? Java I/O Operation - Wrapper Class vs Primitive Class Variables Java Program to Find the Perimeter of a Rectangle Java Thread Priority Java Type Erasure Need of Concurrent Collections in Java Nested ArrayList in Java Print Odd and Even Numbers by Two Threads in Java Square pattern in Java TemporalAdjusters next() method in Java with Examples What does start() function do in multithreading in Java Convert Number to Words Problem in Java Detect And Remove Cycle in a Single Linked List in Java Evolution of Interfaces in Java How to pad a String in Java Implementing Patricia Trie in Java Java Program to Find the Most Repeated Word in a Text File java.util.UUID class in Java ReadWriteLock Interface in Java Reference Data Types in Java Sort An Array According to The Count of Set Bits in Java Alternate Vowel and Consonant string in Java Built-in Exceptions in Java with Examples Capture the Pawns Problem in Java Collections.shuffle() Method in Java with Examples JDBC MySQL Localhost 3306 Alternate Vowel and Consonant string in Java Built-in Exceptions in Java with Examples Capture the Pawns Problem in Java Collections.shuffle() Method in Java with Examples Convert Number to Words Problem in Java Detect And Remove Cycle in a Single Linked List in Java Evolution of Interfaces in Java How to pad a String in Java Implementing Patricia Trie in Java Java Program to Find the Most Repeated Word in a Text File java.util.UUID class in Java ReadWriteLock Interface in Java Reference Data Types in Java Sort An Array According to The Count of Set Bits in Java JDBC MySQL Localhost 3306 Adding a Character as Thousands Separator to Given Number in Java Circular Primes in Java Equilibrium Index Problem in Java Java String regionMatches() Method with Examples LFU Cache Problem in Java Longest Repeating and Non-Overlapping Substring in Java Prefix Suffix Search in Java Product Array Puzzle Problem in Java Russian Doll Envelopes Problem in Java Second Most Repeated Word in a Sequence in Java Special Two-Digit Numbers in a Binary Search Tree in Java Swap Corner Words and Reverse Middle Characters in Java Toggle K bits Problem in Java Upside Down Binary Tree in Java Verbal Arithmetic Puzzle Problem in Java

Java Program to Find the Perimeter of a Rectangle

A Rectangle is a polygon with four sides where all the internal angles are equal to 90°. In the rectangle, only the opposite sides are equal in length, unlike the square, where all the sides are equal. Suppose one side of the rectangle is 15 cm; its opposite side will also be 15 cm. In the below image, the sides A and C and B and D are equal.

Java Program to Find the Perimeter of a Rectangle

The perimeter of a rectangle is defined as the addition of all its sides. Thus, its perimeter can be calculated using the formula below.

Perimeter = A + B + C + D

Since the opposite sides of a rectangle are equal, its perimeter can also be calculated as given below.

Perimeter = A + B + C + D = A + B + A + B = 2A + 2B = 2(A+B)

Example----

Input

 A = 10, B = 5, C = 10, D = 5.

Output

The perimeter of the rectangle is 30

Explanation

2(A + B) = 2(10 + 5) = 30.

Implementation 

Please go through the example below, where we have implemented a Java program to calculate the perimeter of a rectangle.

FileName: RectPerimeter.java

// Java program to calculate the perimeter of a rectangle

import java.util.*;

public class RectPerimeter {

    // main method

    public static void main(String[] args) {

        int a, b, c, d;

        int perimeter;

        a = c = 10;

        b = d = 5;

        perimeter = a + b + c + d;

        // Another formula

        //perimeter = 2 * (a + b);

        // Display the perimeter

        System.out.println("The perimeter of the given rectangle is " +perimeter);

    }

}

Output:

The perimeter of the given rectangle is 30