Infolinks

Breaking

Adsense

Wednesday, January 27, 2016

JAVA TRIANGLE AREA: Solve for the Area of a Triangle

Hi there! The following tutorial will introduce you to one of the most common problems encountered in programming subjects. Later on, I will provide set of activities and quizzes for you to practice on to improve your programming skills.

Area solver for shapes is just one of the exercises given by most programming instructors. Some other exercises are the following:

1. Celsius to Fahrenheit Temperature Converter
2. Identify if odd or even number
3. Display numbers using a for loop
4. Solve for the area of a triangle
5. Fibonacci Series

and many more. These are just some of the activities that I posted here in my blog. You can visit the links if you want.

Area is the size of the surface of an object. In this article, we will discuss about the formula and java code in solving for the area of a triangle.

Formula Area of Triangle













h = height
b = base
A = Area of a triangle

Java Code Area of Triangle

public class Area {

public static void main(String[] args) {

// solve for the area of a triangle given the height and width
double height = 3.5;
double width = 5;
double area;
area=height*width/2;
System.out.println(area);
}
}


Output Area of Triangle






Or you can also achieve it through user input where a user will provide the value for the height and width.

Java Code Area of Triangle

import java.util.Scanner; 
public class Area {

public static void main(String[] args) {

// solve for the area of a triangle given the
//height and width will be provided by user
Scanner console=new Scanner(System.in);
System.out.println("Enter the triangle's width: ");
double height = console.nextDouble();
System.out.println("Enter the triangle's base: ");
double width = console.nextDouble();
double area=height*width/2;
System.out.println("The area of the triangle is "+area);
}
}

Output Area of Triangle







Thank you for visiting and hope this helps you. Feel free to leave a comment below for any questions. I got some other articles related to php and mysql here in my blog. Watch out for more upcoming tutorials related to blogging and programming. Good day! ^_^

JAVA TRIANGLE AREA: Solve for the Area of a Triangle

No comments:

Post a Comment

Adbox