Find Area of a Triangle Using Its Base and Height: C Program

Find the area of the Triangle when it’s base and height are input by the user.

Related Read:
Find Area of a Triangle Using Its Sides: C Program

Formula To Calculate Area of Triangle when its Base and Height are given

Area = (Base * Height) / 2.0;

Note: If we divide an expression or number by 2, it’ll return only the integer part and the decimal part will be discarded. So we are dividing the expression by 2.0 (which is of type double).

Find Area of a Triangle Using Its Base and Height: C Program


[youtube https://www.youtube.com/watch?v=Vk3sB2G2pI8]

YouTube Link: https://www.youtube.com/watch?v=Vk3sB2G2pI8 [Watch the Video In Full Screen.]


Find Area of a Triangle Using Its Base and Height: C Program

#include < stdio.h >

int main()
{
    float base, height, area;

    printf("Enter length of base of Triangle\n");
    scanf("%f", &base);

    printf("Enter length of height of Triangle\n");
    scanf("%f", &height);

    area = (base * height) / 2.0;

    printf("Area of Triangle is %0.2f\n", area);

    return 0;
}

Output:
Enter length of base of Triangle
15
Enter length of height of Triangle
25
Area of Triangle is 187.50

Validate the Input and Find Area of Triangle: C Program

#include < stdio.h >

int main()
{
    float base, height, area;

    printf("Enter length of base of Triangle\n");
    scanf("%f", &base);

    printf("Enter length of height of Triangle\n");
    scanf("%f", &height);

    if(base == 0 || height == 0)
    {
        printf("Invalid Input\n");
    }
    else
    {
        area = (base * height) / 2.0;
        printf("Area of Triangle is %0.2f\n", area);
    }

    return 0;
}

Output 1:
Enter length of base of Triangle
0
Enter length of height of Triangle
25
Invalid Input

Output 2:
Enter length of base of Triangle
15
Enter length of height of Triangle
30
Area of Triangle is 225.00

Note: Note that the area of Triangle has only 2 digits after the decimal point. That is because we have %0.2f as format specifier in the printf method where we are printing out the area of Triangle.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Canvas Basics: HTML5

We shall start exploring more about Canvas element of HTML5.
It’s designed to draw graphics on the fly, using Javascript.

Canvas is like a placeholder for graphics. We place a context upon canvas, upon which we draw our graphics – lines, circles, triangle, pictures etc.

HTML file
index.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
< !DOCTYPE HTML>
<html>
 <head>
  <title>Canvas: HTML5</title>
  <meta charset="utf-8"/>
  <link href="myStyle.css" rel="stylesheet"/>
 </head>
 <body>
 
  <canvas id="holder" width="300" height="200">
    Please upgrade your browser!
  </canvas>
 
</body>
</html>

Here we have attached a style sheet to index.html
and we have the canvas element with an id, width and a height property applied to it.

We can specify some text in-between the opening and closing canvas tag, which shows up in the browser which doesn’t support HTML5’s canvas element.

CSS file
myStyle.css

1
2
3
canvas {
border: 3px groove black;
}

Here we’re applying some basic style to our canvas.

Since canvas is transparent by nature, we can’t see it unless we give it a border or fill some color into it. So, using CSS we apply 3px wide black border, for the purpose of illustration.

Canvas Basics: HTML5


[youtube https://www.youtube.com/watch?v=2lJqB9RikYo]

YouTube Link: https://www.youtube.com/watch?v=2lJqB9RikYo [Watch the Video In Full Screen.]



CSS width and height
We could even assign width and height property of canvas element via CSS, but it is not the proper way to do it.

We must give width and height property inside HTML document itself – as canvas property.

If you assign width and height via CSS, it’ll either stretch or shrink depending on the actual canvas width and height: which looks weird.