Java Program to Calculate Area of Triangle – 5 different ways

 Java Program to Calculate Area of Triangle – 5 different ways

The java program to calculate area of triangle has been written in five different ways such as Scanner class, direct value initialization, Buffered Reader class, Command Line argument, and using Constructor.

Java Program to Calculate Area of Triangle – 5 different ways are as follows:

1. Java Program to calculate Area of Triangle using Scanner class:

Program:

import java.io.*;
import java.util.*;
class TriangleArea
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter Height");
int Height= sc.nextInt();
System.out.println("Enter Base");
int Base= sc.nextInt();
float Area =(Base*Height)/2;
System.out.println("The area of triangle is "+Area);
}
}

Output:

Enter Height
10
Enter Base
10
The area of triangle is 50.0

2. Java Program to calculate Area of Triangle using direct value initialization:

Program:

import java.io.*;
import java.util.*;
class TriangleArea
{
public static void main(String args[])
{
int height=10;
int base=10;
int area=(base*height)/2;
System.out.println("The area of triangle is "+area);
}
}

Output:

The area of triangle is 50

3. Java Program to calculate Area of Triangle using Buffered Reader class:

Program:

import java.util.*;
import java.lang.*;
import java.io.*;
class AreaTriangle
{
public static void main(String args[]) throws Exception
{
float area;
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the height");
float h = Float.parseFloat(br.readLine());
System.out.println("Enter the base");
float b = Float.parseFloat(br.readLine());
area=(b*h)/2;
System.out.println("The area of triangle is "+area);
}
}

Output:

Enter the height
10
Enter the base
10
The area of triangle is 50.0

4. Java Program to calculate Area of Triangle using Command Line Argument:

Program:

import java.util.*;
class AreaTriangle
{
public static void main(String args[])
{
float area;
int b= Integer.parseInt(args[0]);
System.out.println(b);
int h= Integer.parseInt(args[1]);
System.out.println(h);
area=(b*h)/2;
System.out.println("The area of triangle is "+area);
}
}

Compile:

javac AreaTriangle.java

Run:

java AreaTriangle 10 10
The area of triangle is 50.0

5. Java Program to calculate Area of Triangle using Constructor:

Program:

import java.util.*;
class AreaTriangle
{
	double area;
	AreaTriangle(double h, double b)
	{
	 area= (b*h)/2;
 
	}
}
class TrArea
{
   public static void main(String args[]) 
    {   
      Scanner sc= new Scanner(System.in);
      System.out.println("Enter the height");
      double height= sc.nextDouble(); 
      System.out.println("Enter the base");
      double base= sc.nextDouble(); 	  
      AreaTriangle obj =new AreaTriangle(height,base);
	  System.out.println("The Area of Triangle is " + obj.area);      
   }
 }

Output:

Enter the height
10
Enter the base
10
The Area of Triangle is 50.0

 

Java Program to Calculate Perimeter of Triangle – 5 different ways

The java program to calculate perimeter of triangle has been written in five different ways such as Scanner class, direct value initialization, Buffered Reader class, Command Line argument, and using Constructor. Java Program to calculate perimeter of triangle – 5 different ways are as follows:

1. Java Program to calculate Perimeter of Triangle using Scanner class:

Program:

import java.io.*;
import java.util.*;
class CrArea
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter first side");
int s1= sc.nextInt();
System.out.println("Enter second side");
int s2= sc.nextInt();
System.out.println("Enter third side");
int s3= sc.nextInt();
int perimeter=s1+s2+s3;
System.out.println("The perimeter of triangle is "+perimeter);
}
}

Output:

Enter first side
10
Enter second side
10
Enter third side
10
The perimeter of triangle is 30

2. Java Program to calculate Perimeter of Triangle using Direct value Initialization:

Program:

import java.util.*;
import java.lang.*;
import java.io.*;
class TriangleArea
{
public static void main(String args[]) throws Exception
{
int s1=12;
int s2=10;
int s3= 8;
int perimeter=s1+s2+s3;
System.out.println("The perimeter of triangle is "+perimeter);
}
}

Output:

The perimeter of triangle is 20

3. Java Program to calculate Perimeter of Triangle using Buffered Reader class:

Program:

import java.util.*;
import java.lang.*;
import java.io.*;
class TriangleArea
{
public static void main(String args[]) throws Exception
{
double perimeter;
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the first side");
int s1= Integer.parseInt(br.readLine());
System.out.println("Enter the second side");
int s2= Integer.parseInt(br.readLine());
System.out.println("Enter the third side");
int s3= Integer.parseInt(br.readLine());
perimeter=s1+s2+s3;
System.out.println("The perimeter of triangle is "+perimeter);
}
}

Output:

Enter the first side
10
Enter the second side
5
Enter the third side
5
The perimeter of triangle is 20.0

 

4. Java Program to calculate Perimeter of Triangle using Command Line Argument:

Program:

import java.util.*;
class TriangleArea
{
public static void main(String args[])
{
double perimeter;
int s1= Integer.parseInt(args[0]);
int s2= Integer.parseInt(args[1]);
int s3= Integer.parseInt(args[2]);
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
perimeter=s1+s2+s3;
System.out.println("The perimeter of triangle is "+perimeter);
}
}

Compile:

javac TriangleArea.java

Run:

java TriangleArea 10 5 2
The perimeter of triangle is 15.0

5. Java Program to calculate Perimeter of Triangle using Constructor:

Program:

import java.util.*;
class Perimeter
{
	double peri;
	Perimeter(double s1, double s2, double s3)
	{
	 peri=s1+s2+s3;
 
	}
}
class CrArea
{
   public static void main(String args[]) 
    {   
      Scanner sc= new Scanner(System.in);
      System.out.println("Enter the first side");
      double side1= sc.nextDouble();  
      System.out.println("Enter the second side");
      double side2= sc.nextDouble();   
      System.out.println("Enter the third side");
      double side3= sc.nextDouble();   	  
      Perimeter obj =new Perimeter(side1, side2, side3);
	  System.out.println("The perimeter of triangle is " + obj.peri);      
   }
 }

Output:

Enter the first side
10
Enter the second side
5
Enter the third side
1
The perimeter of triangle is 16.0

 

Recommended  Post:

1. Java program to calculate Area and circumference of circle using different ways.

 

Leave a Comment