Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Java Program To Calculate Area and Perimeter of Rectangle ~ foundjava

Java Program for Area and Perimeter of Rectangle


Now here we are gonna to discuss that how to write simple Java program to calculate area and Perimeter of 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);
}
}




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 becuase it is mostly asked in java interview.


This post first appeared on Found Java, please read the originial post: here

Share the post

Java Program To Calculate Area and Perimeter of Rectangle ~ foundjava

×

Subscribe to Found Java

Get updates delivered right to your inbox!

Thank you for your subscription

×