Java program to calculate the area of a triangle when three sides are given or normal method. Calculating the area of a triangle is quite simple if you know the basics of java programming. If you were at newbie level to learn the Java programming then check out the following tutorial so that you will get an idea.
The following program has been written in three different ways using the static method, basic formula when three sides are given, user-defined method, using a constructor with sample outputs.
What is a triangle?
Triangle, is plane-sided or which looks like a pyramid, have three sides and three angles in whatever length or angle it may be, but sum-it-up to 180 degrees.
What is the formula to find the calculate the area of a triangle?
The formula is varied for different types of triangle, but the most common formula that was used as
(Height X Base /2 )
Consider the following program as a sample method – 1, there. There were more than 2 methods here listed below check it out. Moreover, if you have any doubts related to this section, then do comment at the end of the post we are glad to help you out.
5 Different Ways To Find Area Of Triangle
Sample method -1 # To find or calculate the area of a triangle #
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import java.util.Scanner; class AreaOfTriangle { public static void main(String args[]) { Scanner s= new Scanner(System.in); System.out.println("Enter the width of the Triangle:"); double b= s.nextDouble(); System.out.println("Enter the height of the Triangle:"); double h= s.nextDouble(); //Area = (width*height)/2 double area=(b*h)/2; System.out.println("Area of Triangle is: " + area); } } |
Output :
1 2 3 4 5 |
Enter the width of the Triangle: 10 Enter the height of the Triangle: 20 Area of Triangle is:100.0 |
Online Execution & Compiler Tool To Calculate Area
If you were new to java programming, then check out the below explanation about how the above java code works. If you knew the basic level of programming, then skip it and move on to second sample method with online execution and compiler too.
1)
1 |
import java.util.Scanner; |
– Type which passes the primitive types and strings.
2)
1 |
class AreaOfTriangle |
– In the real world, you’ll often find many individual objects all of the same kind. There may be thousands of other bicycles in existence, all of the same make and model.
Each bicycle was built from the same set of blueprints and therefore contains the same components. In object-oriented terms, we say that your bicycle is an instance of the class of objects known as bicycles.
A class is a blueprint from which individual objects are created.
3)
1 |
public static void main(String args[]) |
– the Main function, the main program will start reading the values from here. Consider it has a starting function of the main block.
4)
1 |
Scanner s= new Scanner(System.in); |
– Scanner read the values from the main java.util.Scanner; or consider it has read the inputs values, whereas system.in is to read the values from your system or device.
5)
1 |
System.out.println("Enter the width of the Triangle:"); |
– Displays whatever you were written.
6)
1 |
double b= s.nextDouble(); |
-is a datatype.
7)
1 |
double area=(b*h)/2; |
– The formula to find the area of a triangle.
8)
1 |
System.out.println("Area of Triangle is: " + area); |
– The output will be displayed here.
1 2 3 4 5 |
Enter the width of the Triangle: 12 Enter the height of the Triangle: 15 Area of Triangle is: 90 |
2. Java Program Calculate Area Of Triangle Using Constructor
There you go another method, using the constructor with sample output example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
import java.util.Scanner; class AOT { long area; AOT(long b,long h) { area=(b*h)/2; } } class Xyz { public static void main(String args[]) { Scanner s= new Scanner(System.in); System.out.println("Enter the width of the Triangle:"); long b= s.nextLong(); System.out.println("Enter the height of the Triangle:"); long h= s.nextLong(); AOT A1=new AOT(b,h); System.out.println("Area of Triangle is: " + A1.area); } } |
1 2 3 4 5 |
Enter the width of the Triangle: 3 Enter the height of the Triangle: 4 Area of Triangle is: 6 |
3. Using User-Defined Method
Another method, using user-defined method or function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
import java.util.Scanner; class Xyz { public static void main(String args[]) { Scanner s= new Scanner(System.in); System.out.println("Enter the width of the Triangle:"); double b= s.nextDouble(); System.out.println("Enter the height of the Triangle:"); double h= s.nextDouble(); double area=AOT(b,h); System.out.println("Area of Triangle is: " + area); } static double AOT(double b,double h) { return ((b*h)/2); } } |
output:
1 2 3 4 5 |
Enter the width of the Triangle: 5 Enter the height of the Triangle: 3 Area of Triangle is: 7.5 |
4.Using when three sides are given
Basically, In order to calculate the area, you need to find out the Height of the triangle. If you don’t know the height or you may have no idea how to find out the height of the triangle, then you can use the below program to calculate the area of a triangle.
When three sides are given we have used the following formula: only when three sides are given.
s(s-a)(s-b)(s-c)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
import java.util.Scanner; class AreaOfTriangle3 { public static void main(String args[]) { Scanner s1= new Scanner(System.in); System.out.println("Enter the 1st side:"); int a= s1.nextInt(); System.out.println("Enter the 2nd side:"); int b= s1.nextInt(); System.out.println("Enter the 3rd side:"); int c= s1.nextInt(); if((a+b)>c && (a+c)>b && (b+c)>a) { int s=(a+b+c)/2; double area=Math.sqrt(s*(s-a)*(s-b)*(s-c)); System.out.println("Area of Triangle is: " + area); } else System.out.println("Area of Triangle not exit"); } } |
1 2 3 4 5 6 7 |
Enter the 1st side: 10 Enter the 2nd side: 10 Enter the 3rd side: 10 Area of Triangle is:43.30 |
Online Execution Tool for the above java code:
Above one is another simple method to find the area of the triangle here the formula is :
s(s-a)(s-b)(s-c)
Where the value of S is {( a+b+c)/2} and the loop method as ” if((a+b)>c && (a+c)>b && (b+c)>a) ”
The above example, we have used the datatype “Int”. It’s all up to you, you can even use either float or double.
Why ” Double ” but not ” Int”?
Every type of data types has significant memory storage capacity, for instance in, the Int can store the memory up to 4 bytes, whereas ” Double ” Can store the memory values up to 8-bytes.
If you have any doubts or any bugs leave a reply here.