Java Program to Calculate Area of Square, Rectangle and Triangle

In this tutorial, we will write below Java programs:
  1. Java Program to Calculate Area of Square
  2. Java Program to Calculate Area of Rectangle
  3. Java program to calculate the area of Triangle

1. Java Program to Calculate Area of Square

package com.javaguides.java.tutorial;

import java.util.Scanner;

/**
 * Java Program to Calculate Area of Square
 * 
 * @author https://www.javaguides.net/
 *
 */
public class JavaProgram {
    public static void main(String[] args) {

        try (Scanner scanner = new Scanner(System.in)) {
            System.out.println("Enter Side of Square:");
            // Storing the captured value in a variable
            double side = scanner.nextDouble();
            // Area of Square = side*side
            double area = side * side;
            System.out.println("Area of Square is: " + area);
        }
    }
}
Output:
Enter Side of Square:
10
Area of Square is: 100.0

2. Java Program to Calculate Area of Rectangle

package com.javaguides.java.tutorial;

import java.util.Scanner;

/**
 * Java Program to Calculate Area of Rectangle
 * 
 * @author https://www.javaguides.net/ *
 */
public class JavaProgram {
    public static void main(String[] args) {

        try (Scanner scanner = new Scanner(System.in)) {
            System.out.println("Enter the length of Rectangle:");
            double length = scanner.nextDouble();
            System.out.println("Enter the width of Rectangle:");
            double width = scanner.nextDouble();
            // Area = length*width;
            double area = length * width;
            System.out.println("Area of Rectangle is:" + area);
        }
    }
}
Output:
Enter the length of Rectangle:
10
Enter the width of Rectangle:
20
Area of Rectangle is:200.0

3. Java program to calculate the area of Triangle

package com.javaguides.java.tutorial;

import java.util.Scanner;

/**
 * Java program to calculate area of Triangle
 * 
 * @author https://www.javaguides.net/ *
 */
public class JavaProgram {
    public static void main(String[] args) {

        try (Scanner scanner = new Scanner(System.in)) {
            System.out.println("Enter the width of the Triangle:");
            double base = scanner.nextDouble();

            System.out.println("Enter the height of the Triangle:");
            double height = scanner.nextDouble();

            // Area = (width*height)/2
            double area = (base * height) / 2;
            System.out.println("Area of Triangle is: " + area);
        }
    }
}
Output:
Enter the width of the Triangle:
10
Enter the height of the Triangle:
5
Area of Triangle is: 25.0

Related Java Programs

Comments

  1. Thanks for sharing this article here about the square one auto. Your article is very informative and I will share it with my other friends as the information is really very useful. Keep sharing your excellent work.square one auto

    ReplyDelete

Post a comment