Java Program to Find the Area and Perimeter of Rectangle using Class

This is a Java Program to Create a Simple Class to Find out the Area and Perimeter of Rectangle.
Formula:
Perimeter of rectangle = 2 * (length + breadth)
Area of rectangle = length * breadth

Enter the length and breadth of the rectangle as input. Now we use the given formula to calculate the area and perimeter of rectangle.

Here is the source code of the Java Program to Create a Simple Class to Find out the Area and Perimeter of Rectangle. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. import java.util.Scanner;
  2. public class Area_Perimeter 
  3. {
  4.     public static void main(String[] args) 
  5.     {
  6.         int l, b, perimeter, area;
  7.         Scanner s = new Scanner(System.in);
  8.         System.out.print("Enter length of rectangle:");
  9.         l = s.nextInt();
  10.         System.out.print("Enter breadth of rectangle:");
  11.         b = s.nextInt();
  12.         perimeter = 2 * (l + b);
  13.         System.out.println("Perimeter of rectangle:"+perimeter);
  14.         area = l * b;
  15.         System.out.println("Area of rectangle:"+area);
  16.     }
  17. }

Output:

$ javac Area_Perimeter .java
$ java Area_Perimeter 
 
Enter length of rectangle:4
Enter breadth of rectangle:5
Perimeter of rectangle:18
Area of rectangle:20

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement

Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.