BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java program to calculate area of Triangle

Last Updated: September 10, 2022 by Chaitanya Singh | Filed Under: Java Examples

Here we will see how to calculate area of triangle. We will see two following programs to do this:
1) Program 1: Prompt user for base-width and height of triangle.
2) Program 2: No user interaction: Width and height are specified in the program itself.

Program 1:

/**
 * @author: BeginnersBook.com
 * @description: Program to Calculate area of Triangle in Java
 * with user interaction. Program will prompt user to enter the 
 * base width and height of the triangle.
 */
import java.util.Scanner;
class AreaTriangleDemo {
   public static void main(String args[]) {   
      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:
2
Enter the height of the Triangle:
2
Area of Triangle is: 2.0

Program 2:

/**
 * @author: BeginnersBook.com
 * @description: Program to Calculate area of Triangle
 * with no user interaction.
 */
class AreaTriangleDemo2 {
   public static void main(String args[]) {   
      double base = 20.0;
      double height = 110.5;
      double area = (base* height)/2;
      System.out.println("Area of Triangle is: " + area);      
    }
}

Output:

Area of Triangle is: 1105.0

Top Related Articles:

  1. Java Program to Calculate average using Array
  2. Java Program to remove duplicate elements in an Array
  3. Java Program to Print Right Pascal’s Triangle Pattern
  4. Java program to reverse a number using for, while and recursion
  5. Java Program to Print Diamond Pattern

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Comments

  1. Saurabh Yadav says

    June 16, 2015 at 10:46 PM

    *
    * *
    * * *
    * * * *
    * * * * *
    completely coding

    Reply
    • chinna says

      November 26, 2015 at 11:56 AM

      package coreJava;
      /**
      * @author Narayana
      *
      */
      public class Triangle {
      public static void main(String[] args) {
      int n=5;
      for (int i=1;i<=n ;i++ ){
      for (int j=1;j<=i ;j++ ){
      System.out.print("*");
      }
      System.out.println( );
      }
      }
      }

      Reply

Leave a Reply Cancel reply

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

Java Examples

  • Check Odd-even
  • Linear Search
  • Binary Search
  • Floyd's Triangle
  • Reverse number
  • Random Number
  • first n prime numbers
  • Disp prime Numbers
  • Check Prime number
  • Palindrome String
  • Find factorial
  • Sum of elements of Array
  • Area of rectangle
  • Area of Square
  • Area of Triangle
  • Circle

Tutorials

  • Java Tutorial
  • OOPs Concepts
  • Java String
  • Exception handling
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap