C Program To Find Area, Perimeter and Semi-perimeter: Valid Triangle

Lets ask the user to input lengths of 3 sides of the Triangle. First lets check if those 3 values form a valid Triangle. If it does, then we shall calculate perimeter, semi-perimeter and area of the Triangle. If it doesn’t form a valid Triangle, then we display that message on to the console window.

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

Logic To check if it’s a valid Triangle or Not

Logic to find if the user entered values for sides of Triangle actually forms a valid Triangle or not is present in our previous video tutorial. Please watch it without fail before continuing this tutorial. We’ve posted source code, video and explanation at Triangle Valid or Not based On Sides: C Program

Formula To Calculate Perimeter, Semi-perimeter and Area of a Triangle

p = (a + b + c);
sp = (a + b + c) / 2.0;
area = sqrt( sp * (sp-a) * (sp-b) * (sp-c) );

where a, b and c are lengths of sides of the Triangle.
p – perimeter
sp – Semi-perimeter.

C Program To Find Area, Perimeter and Semi-perimeter: Valid Triangle


[youtube https://www.youtube.com/watch?v=CmGb-nlSC7o]

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


C Program To Find Area, Perimeter and Semi-perimeter: Valid Triangle

#include < stdio.h >
#include < math.h >

int main()
{
    float a, b, c, flag = 0, p, sp, area;

    printf("Enter values for a, b and c\n");
    scanf("%f%f%f", &a, &b, &c);

    if( a>b && a>c)
    {
        flag = ((b+c) > a);
    }
    else if( b>c )
    {
        flag = ((a+c) > b);
    }
    else
    {
        flag = ((a+b) > c);
        printf("%f\n", flag);
    }

    if(flag)
    {
        p    = a + b + c;
        sp   = p / 2.0;
        area = sqrt( sp*(sp-a)*(sp-b)*(sp-c) );

        printf("Perimeter is %f\n", p);
        printf("Semi-Perimeter is %f\n", sp);
        printf("Area of Triangle is %f\n", area);
    }
    else
    {
        printf("Invalid Triangle\n");
    }

    return 0;
}

Output 1:
Enter values for a, b and c
10
5
3
Invalid Triangle

Output 2:
Enter values for a, b and c
5
6
9
Perimeter is 20.000000
Semi-Perimeter is 10.000000
Area of Triangle is 14.142136

In above source code, variable flag stores true(1) or false(0) value.

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

Triangle Valid or Not based On Sides: C Program

If the three sides of a Triangle are entered through the keyboard, write a C program to check whether the Triangle is valid or not. The Triangle is valid if the sum of two sides is greater than the largest of the three sides.

Related Read:
Basic Arithmetic Operations In C
Find Area of a Triangle Using Its Sides: C Program
Triangle Valid or Not based On Angles: C Program

Logic To Find Valid Triangle or Not

First we find out biggest side in the 3 sides of the triangle. Next we add the other 2 sides. Now the addition of the other 2 sides must be greater than the biggest side of the Triangle, for a Triangle to be valid. If not, its not a Triangle.

Example:
If a, b and c are 3 sides of the Triangle. If c is the largest side. Then for the Triangle to be valid, (a+b) must be greater than c.
(a+b) > c
If this is true, then Triangle is valid, if not, its invalid Triangle.

Triangle Valid or Not based On Sides: C Program


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

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


Triangle Valid or Not based On Sides: C Program

#include < stdio.h >

int main()
{
    float a, b, c, flag = 0;

    printf("Enter values for a, b and c\n");
    scanf("%f%f%f", &a, &b, &c);

    if( a>b && a>c )
    {
        flag = ((b+c) > a);
    }
    else if( b>c )
    {
        flag = ((a+c) > b);
    }
    else
    {
        flag = ((a+b) > c);
    }

    if(flag)
    {
        printf("Valid Triangle\n");
    }
    else
    {
        printf("Invalid Triangle\n");
    }

    return 0;
}

Output 1:
Enter values for a, b and c
10
4
3
Invalid Triangle

Output 2:
Enter values for a, b and c
10
15
6
Valid Triangle

In above source code, variable flag stores true(1) or false(0) value.

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

Rules for Constructing Variable Names: C

Rules for constructing variable names are same for all the data types.

Related Read:
Rules for Constructing int, float, char constants: C

Rules for Constructing Variable Names: C


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

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


Rules for constructing variable names

1. First character in a variable name must be an alphabet or an underscore( _ ).

2. Variable name can have alphabet, digits and underscore.

3. No commas, space allowed in variable name.

4. No other special symbols other than underscore is allowed.

5. C variables are case sensitive. Ex: name and Name are two different variables.

6. You can not use keywords / reserve words as variable name in C.

Valid Variable Names
_name
user_name
age20
iMac

Invalid Variable Names
9hundred
Micro soft
Apple$computers

Note: All these rules for constructing variable names apply for all the data types in C.

Pseudo-classes for Form Element

CSS3 introduced some new pseudo-classes for form elements. In this video tutorial we’ll be looking at these 3 pseudo-classes:
:required
:valid
:invalid

required-fields-css3-html5

HTML file
index.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
< !DOCTYPE HTML>
<html>
<head>
<title>CSS pseudo-classes for form elements</title>
<meta charset="utf-8"/>
<link href="myStyle.css" rel="stylesheet"/>
</head>
<body>
 
<form>
Text: <input type="text" required/><br />
Email: <input type="email" placeholder="[email protected]"/><br />
<input type="submit" value="done!"/>
</form>
 
</body>
</html>

Here we have attached a stylesheet where we write css3 form element pseudo-class definition. Here we also take 2 input fields of type text and email respectively. We also make the text field as required. Also add a placeholder to the email input field to provide hint to the user.

CSS file
myStyle.css

1
2
3
4
5
6
7
8
9
10
11
input:required {
background-color: yellow;
}
 
input:valid {
background-color: lightblue;
}
 
input:invalid {
background-color: red;
}

Here the fields which has been specified as required will get a background-color of yellow. valid entries will have a lightblue input field background color, and invalid entry input fields background turns red color.

CSS3 pseudo-classes For Form Element: HTML5


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

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



Note:
CSS3 introduces new form elements like email, number, date etc ..all these fields validate themselves. We need not write lengthy JavaScript code to make small validations – like, checking if the user entered numeric value in the input field marked as age. Since age input field will have a numeric input type, if the user enters any string, we can let the user know about the wrong entry by making use of these css3 introduced pseudo-classes.

Also note that, if you try to submit the form without filling the required fields, you’ll get a little tool tip like message interface, telling you to fill the form field.