codescracker


java

Java Program to Calculate Area and Perimeter of Square and Rectangle



« Previous Program Next Program »


Calculate Area and Perimeter of Square and Rectangle

To calculate the area and perimeter of a square and rectangle in Java Programming, you have to ask to the user to enter length and breadth of the rectangle and side length of the square and make two variable, one for area and one for perimeter for each to perform the mathematical calculation and display the result on the output screen.

Java Programming Code to Calculate Area and Perimeter of Square and Rectangle

Following are one by one Java programs to calculate the area and the perimeter of Square and Rectangle.

Java Programming Code to Calculate Area and Perimeter of Rectangle

Following Java program ask to the user to enter the length and breadth of the rectangle to calculate the area and perimeter of the rectangle and display the result :

/* Java Program Example - Calculate Area Perimeter of Rectangle */
		
import java.util.Scanner;

public class JavaProgram
{
    public static void main(String args[])
    {
        int len, bre, peri, area;
        Scanner scan = new Scanner(System.in);
		
        System.out.print("Enter Length and Breadth of Rectangle : ");
        len = scan.nextInt();
        bre = scan.nextInt();
		
        area = len*bre;
        peri = (2*len) + (2*bre);
		
        System.out.print("Area = " +area);
		
        System.out.print("\nPerimeter = " +peri);
    }
}

When the above Java Program is compile and executed, it will produce the following output:

Java Program calculate area perimeter of rectangle

Java Programming Code to Calculate Area and Perimeter of Square

Following Java Program ask to the user to enter the side of a square to calculate and display the area and perimeter of the square:

/* Java Program Example - Calculate Area Perimeter of Square */
		
import java.util.Scanner;

public class JavaProgram
{
    public static void main(String args[])
    {
        int side, peri, area;
        Scanner scan = new Scanner(System.in);
		
        System.out.print("Enter Side Length of Square : ");
        side = scan.nextInt();
		
        area = side*side;
        peri = 4*side;
		
        System.out.print("Area = " +area);
		
        System.out.print("\nPerimeter = " +peri);
    }
}

When the above Java Program is compile and executed, it will produce the following output:

Java Program calculate area perimeter of square

Same Program in Other Languages

You may also like to learn and practice the same program in other popular programming languages:


« Previous Program Next Program »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test