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

Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours


Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course

Comments

  1. This is a great inspiring article.I am pretty much pleased with your good work.You put really very helpful information. Keep it up. Keep blogging. Looking to reading your next post. sales velocity formula

    ReplyDelete

Post a Comment