ADVERTISEMENT
ADVERTISEMENT

Java program to find Area of Triangle

Area of Triangle in Java - This program will read base and height of the Triangle and Calculate Area of the Triangle.

To find Area of the Triangle - Multiply Triangle Base width with Triangle Height.

Area of Triangle using Java program

//Java program to find Area of Triangle.
 
import java.util.*;
 
public class AreaTriangle{
 
     public static void main(String []args){
        double base,height,area;
         
        Scanner sc=new Scanner(System.in);
         
        //Read Length and Width of Triangle
        System.out.print("Enter Base Widht: ");
        base=sc.nextDouble();
        System.out.print("Enter Height: ");
        height=sc.nextDouble();
         
        //Calculate Area
        area = (base*height)/2;
         
        //Print Result
        System.out.println("Area of Triangle: " + area);    
         
     }
}

Output

    
    [email protected]:~$ javac AreaTriangle.java 
    [email protected]:~$ java AreaTriangle 

    Enter Base Widht: 2.3 
    Enter Height: 32.2
    Area of Triangle: 37.03

ADVERTISEMENT

Using Function/Method

//Java program to find Area of Triangle.
 
import java.util.*;
 
public class AreaTriangle{
 
    //Function to find Area of Trianle
    public static double AreaOfTriangle(double b, double h)
    {
        double area;
        area= (b*h)/2;
        return area;
    }
     
     public static void main(String []args){
        double base,height,area;
         
        Scanner sc=new Scanner(System.in);
         
        //Read Length and Width of Triangle
        System.out.print("Enter Base Widht: ");
        base=sc.nextDouble();
        System.out.print("Enter Height: ");
        height=sc.nextDouble();
         
        //Find Area - Calling Functionn
        area= AreaOfTriangle(base, height);
         
        //Print Result
        System.out.println("Area of Triangle: " + area);    
         
     }
}

Core Java Example Programs »


ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.