Java Program for Area of Square

Java Program for Area of Square

Here we are gonna to see java program to find area of square and perimeter of square also with simple examples.

Before calculating area and perimeter of square, let’s focus on some formulas we will use in coming examples.

Formula for area of a square is given below:

area = side * side


Formula for perimeter of a square is given below:

perimeter = 4 * side


Let’s calculate the area of square in java with different-different examples.

1) Area of Square in Java Example 1

This is simple java program to calculate are of square where side are given.

class AreaOfSquare1
{
public static void main(String args[])
{
double side = 6.5;//given side
double area = side * side;//using formula for area of a square 
System.out.println(“Area of Square is : “+area);
}
}

Output: Area of Square is : 42.25

Now moving to next example where we use Scanner class for reading the data or input from the keyboard by the user.

2) Area of Square in Java Example 2

This is another example of area of square where the value of side of square is given by the user form the console and then by the help of square formula we will calculate area of square.

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

System.out.println(“Enter side of square”);
double length = sc.nextDouble();
double area = length * length;

System.out.println(“Area of Square is : “+area);
}
}

You can check for practice other java programs e.g How to calculate area and perimeter of rectangle and how to find area and circumference of cirlcle, etc.

3) Find the Perimeter of a Square in Java Example 3

This our final program in this article where we calculate both area and perimeter of square by using formulas.

import java.util.Scanner;
class PerimeterOfSquare3
{
public static void main(String args[])
{
double side, area, perimeter;

Scanner sc = new Scanner(System.in);

System.out.println(“Enter side of Square”);
side = nextDouble();
area = side * side;
perimeter = 4 * side;

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

Output: Enter side of Square
             6.5
             Area of Square is : 42.25
             Perimeter of Square is : 26.0


In this article we have learned how to find the area of a square and how to find the perimeter of a square in java programming with simple examples.




Source link

Java Program To Find Area of Square

Leave a Reply

Your email address will not be published. Required fields are marked *