Let's learn java programming language with easy steps. This Java tutorial provides you complete knowledge about java technology.

Monday, 4 December 2017

Java Program To Calculate Area and Perimeter of Rectangle

Java Program for Area and Perimeter of Rectangle

calculate area and perimeter of rectangle in java

Now here we are gonna to discuss that how to write a simple java program to calculate area and perimeter of the rectangle. Here we will see some useful programs for calculating area and perimeter of rectangle which is quite useful in any java programming interviews.

Let's see java program to find area of rectangle first and then programs for perimeter of rectangle.

Here we will see some different-different java programs for area of rectangle in java.

Before learning java programs for area and perimeter of rectangle, let's discuss about some useful formula we will use for calculating area and perimeter.

Formula for Area of Rectangle

Area = length * width//This is area rectangle formula

Formula for Perimeter of a Rectangle

Perimeter = 2 * (length + width)//This is perimeter rectangle formula




1) Calculate Rectangle Area Using Java Example 1

This is simple example, where we will use Scanner class to take values of length and width of rectangle from the user and then calculate the area of rectangle.

import java.util.Scanner;
class AreaOfRectangle1
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);

System.out.println("Enter length of rectangle");
double length = sc.nextDouble();

System.out.println("Enter width of rectangle");
double width = sc.nextDouble();

double area = length * width;
System.out.println("Area of rectangle is : "+area);
}
}

Output: Enter length of rectangle
             5
             Enter width of rectangle
             9
             Area of rectangle is : 45.0


2) Calculatea Rectangle Area Using Java Example 2

In this example, We will find the rectangle's area by using method.

import java.util.Scanner;
class AreaOfRectangle2
{
static double rectangleArea(double l, double w)
{
double area;
area = l * w;
return area;
}

public static void main(String args[])
{
double length;
double width;
double area;

Scanner sc = new Scanner(System.in);

System.out.println("Enter length of rectangle");
length = sc.nextDouble();

System.out.println("Enter width of rectangle");
width = sc.nextDouble();

area = rectangleArea(length , width);
System.out.println("Area of rectangle is : "+area);
}
}


You can check other calculating area and perimeter programs in java e.g How to find area and perimeter of circle and how to find area of triangle.

Let''s see our final program which is how do you find the perimeter of  a rectangle by using java program.


3) How to Find the Perimeter of a Rectangle

In this example, We will see how to find the perimeter of a rectangle by using rectangle perimeter formula.

import java.util.*;
class PerimeterOfRectangle
{
public static void main(String args[])
{
double length, width, area, perimeter;

Scanner sc = new Scanner(System.in);

System.out.println("Enter length of rectangle");
length = sc.nextDouble();

System.out.println("Enter width of rectangle");
width = sc.nextDouble();

area = length * width;
perimeter = 2 * (length + width);

System.out.println("Area of rectangle is : "+area);
System.out.println("Perimeter of rectangle is : "+perimeter);
}
}

Output: Enter length of rectangle
             5
             Enter width of rectangle
             9
             Area of rectangle is : 45.0
             Perimeter of rectangle is : 28.0

Calculate area and perimeter of rectangle is quite important for core java interviews because it is mostly asked in java interview.
  
Share:

1 comment:

  1. I was more than happy to uncover this great site. I need to to thank you for your time due to this fantastic read!! I definitely enjoyed every bit of it and I have you bookmarked to see new information on your blog.
    Java Training in Bangalore

    ReplyDelete

Facebook Page Likes

Follow javatutorial95 on twitter

Popular Posts

Translate