Free Data Structures and Algorithms Course









Subscribe below and get all best seller courses for free !!!










OR



Subscribe to all free courses

How to Calculate Area and Perimeter of Rectangle in a Java Program

Program to demonstrate how to Calculate Area and Perimeter of Rectangle in Java.



 

package com.hubberspot.example;

// Import java.util.Scanner to read data from
// console
import java.util.Scanner;

public class RectangleAreaPerimeterDemo {

 public static void main(String[] args) {

  // Creating a Scanner object using a constructor
  // which takes the System.in object. There are various
  // constructors for Scanner class for different
  // purposes. We want to use Scanner class to read
  // data from console on the system, So we are using
  // a constructor which is taking an System.in object
  Scanner scanner = new Scanner(System.in);

  // Create three variables which will hold the
  // values for length, breadth , area and perimeter of Rectangle.
  int length = 0;
  int breadth = 0;
  int area = 0;
  int perimeter = 0;

  // prompt the user to enter value of length of
  // Rectangle 
  System.out.print("Enter the length of Rectangle : ");

  // store the length into length variable entered by user
  length = scanner.nextInt();

  // prompt the user to enter value of breadth of
  // Rectangle
  System.out.print("Enter the breadth of Rectangle : ");

  // store the breadth into breadth variable entered by user
  breadth = scanner.nextInt();

  // Calculate area of Rectangle by multiplying length into breadth.
  area = length * breadth;

  // Calculate perimeter of Rectangle by 2 * (length + breadth)
  perimeter = 2 * (length + breadth);

  // Output area and perimeter to the console
  System.out.println("Area of Rectangle is : " + area);
  System.out.println("Perimeter of Rectangle is : " + perimeter);
 }
}


Output of the program : 


 

Collections Framework API

  1. How to use ArrayList in Java with example ?.
  2. How to use Iterator Interface to iterate/traverse Java Collections ?.
  3. How to perform operations of Union and Intersection on two or more LinkedList in Java ?.
  4. How to implement ListIterator Interface in Java Collections ?.
  5. How to use Vector in Java with example ?.
  6. How to implement Comparator Interface in Java with an example ?
  7. How to use LinkedList in Java with example ?.
  8. How to use TreeSet in Java with example ?.
  9. How to use HashSet in Java with example ?.
  10. How to use Hashtable in Java with example ?.
  11. How to sort and partial sort a primitive arrays using Arrays.sort method in Java ?.
  12. How to perform Binary Search for an element over primitive arrays in Java ?
  13. A simple Java program to sort elements in an ArrayList
  14. How to create Generic Type specific Collections in Java ?
  15. How to hold your objects using Collection Interface in Java ?
  16. How do I add an element and get an element in an ArrayList at specified index in Java ?.
  17. How to add or group one collection into another collection in Java ?.
  18. How to print collections in the Collections Framework on the console in Java ?.
  19. How to compare two or more arrays equality in Java ?.
  20. How to convert an Array into a Set in Java ?.
  21. How to check whether an ArrayList contains an element specified in Java ?.

Database Programming in Java

 
© 2021 Learn Java by Examples Template by Hubberspot