Java Program to Calculate Area of Triangle
In this java tutorial, We are going to learn java program to find area of triangle with 3 different-different ways with simple examples.
There are many method to calculate area of triangle in java programs but here we will see only 3 useful method step-by-step with quite easy examples.
There are many method to calculate area of triangle in java programs but here we will see only 3 useful method step-by-step with quite easy examples.
Let's see java program for area of triangle.
1) Area of Triangle in Java with Example 1
This is the first area of triangle program in java where we will calculate the area of triangle by using area of a triangle formula.
Area of triangle formula is = base * height/2
Let's know the area of triangle through java program by using above formula.
class Demo1
{
public static void main(String args[])
{
double base, height, area;
base = 20.7;//suppose base of triangle is 20.7
height = 15.3;//suppose height of triangle is 15.3
area = base * height/2;
System.out.println("Area of Triangle is : "+area);
}
}
Output: Area of Triangle is : 158.355
Let's take another simple program to find area of triangle in java where we will use Scanner class to take input to the user from the keyboard.
Area of triangle formula is = base * height/2
Let's know the area of triangle through java program by using above formula.
class Demo1
{
public static void main(String args[])
{
double base, height, area;
base = 20.7;//suppose base of triangle is 20.7
height = 15.3;//suppose height of triangle is 15.3
area = base * height/2;
System.out.println("Area of Triangle is : "+area);
}
}
Output: Area of Triangle is : 158.355
Let's take another simple program to find area of triangle in java where we will use Scanner class to take input to the user from the keyboard.
2) Area of Triangle in Java with Example 2
This is simple java program to get the area of triangle where we will take the value of base width and height of triangle from the user.
import java.util.Scanner;
class Demo2
{
public static void main(String args[])
{
//create scanner class object
Scanner sc = new Scanner(System.in);
System.out.println("Enter the base of triangle");
double b = sc.nextDouble();
System.out.println("Enter the height of triangle");
double h = sc.nextDouble();
double area = b * h/2;
System.out.println("Area of triangle is : "+area);
}
}
Output: Enter the base of triangle
48.6
Enter the height of triangle
50.2
Area of triangle is : 1219.8600000000001
Now we are going to take final example of our area of triangle java program.
double area = b * h/2;
System.out.println("Area of triangle is : "+area);
}
}
Output: Enter the base of triangle
48.6
Enter the height of triangle
50.2
Area of triangle is : 1219.8600000000001
Now we are going to take final example of our area of triangle java program.
3) Area of Triangle in Java with Example 3
This is simple java program to find area of triangle using constructor and scanner class.
import java.util.Scanner;
class AreaOfTriangle
{
double area;
AreaOfTriangle(double base, double height)//Constructor declaration
{
area = base * height/2;
System.out.println("Area of Triangle is : "+area);
}
}
class Test
{
public static void main(String args[])
{
//creating Scanner class
Scanner sc = new Scanner(System.in);
System.out.println("Enter base of triangle");
double base = sc.nextDouble();
System.out.println("Enter height of triangle");
double height = sc.nextDouble();
//creating object of AreaOfTriangle class
AreaOfTriangle a = new AreaOfTriangle(base, height);
}
}
Output: Enter base of triangle
5
Enter height of triangle
6
Area of triangle is : 15.0
Read More :
Java Program to Find Area of Square.
Java Program to Calculate Area and Circumference of Circle.
Java Programming Interview Questions.
Here we have discussed, How to calculate area of triangle through java programs with difference - different methods e.g through constructor, Scanner class, etc.
Output: Enter base of triangle
5
Enter height of triangle
6
Area of triangle is : 15.0
Read More :
Java Program to Find Area of Square.
Java Program to Calculate Area and Circumference of Circle.
Java Programming Interview Questions.
Here we have discussed, How to calculate area of triangle through java programs with difference - different methods e.g through constructor, Scanner class, etc.
0 comments:
Post a Comment