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

Find Area of a Triangle Using Its Sides: C Program

If lengths of three sides of a Triangle are input through the keyboard, write a program to find the area of the Triangle.

Related Read:
Basic Arithmetic Operations In C
Triangle Valid or Not based On Sides: C Program

Note:A Triangle is valid if the sum of 2 sides of the Triangle is greater than the largest of the 3 sides.

Important: In this program we assume that the user has entered valid lengths of the Triangle.

Formula To Calculate Area and Semi-perimeter of a Triangle

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.
sp – Semi-perimeter.

Note: In geometry, above formula to calculate area of Triangle is called Heron’s formula (sometimes called Hero’s formula).

Find Area of a Triangle Using Its Sides: C Program


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

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


Find Area of a Triangle Using Its Sides: C Program

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

int main()
{
    float a, b, c, sp, area;

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

    sp = (a+b+c)/2.0;

    area = sqrt(sp*(sp-a)*(sp-b)*(sp-c));

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

    return 0;
}

Output:
Enter values of a, b and c
4
5
6
Area of triangle is 9.921567

Note: While calculating Semi-perimeter, make sure to divide by 2.0 and not by 2. Because division by integer number returns only the integer part of the result. So for certain inputs the result might be wrong. So always use the floating/double value while dividing in C Programs.

Note: Since we are using sqrt() method, we need to include math.h library file (header file) without fail.

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

Area of Rectangle: C Program

The length and breadth / width of a rectangle are entered through the keyboard. Write a program to calculate the area of the rectangle.

Rectangle is a quadrilateral(i.e., it has 4 sides). And all the 4 angles of the rectangle are of 90 degree. To calculate area of a rectangle we need its length and width/breadth.

Related Read:
Basic Arithmetic Operations In C
Perimeter of Rectangle: C Program

Note: Area of a Rectangle is calculated using the formula
Area = Length * Width;


area of rectangle

Length: is the length of the longest side of the rectangle.
width: is the length of the smallest side of the rectangle.

Find Area of a Rectangle: C Program


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

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


Source Code: Find Area of a Rectangle: C Program

#include < stdio.h >

int main()
{
    float length, width, area;

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

    printf("Enter width of rectangle\n");
    scanf("%f", &width);

    area = length * width;

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

    return 0;
}

Output:
Enter length of rectangle
12
Enter width of rectangle
6
Area of Rectangle is 72.000000

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

Calculate Area of a Circle without using math.h library: C

We can calculate area of circle if we know the value of its radius. So using that, we shall write the logic in c program to calculate the area of a circle, without using math.h library functions.

Related Read:
Basic Arithmetic Operations In C

 
#include < stdio.h >

int main()
{
    float radius;
    const float PI = 3.14;

    printf("Enter the value for radius \n");
    scanf("%f", &radius);

    printf("Area of the circle is %f\n", (PI * radius * radius));

    return 0;
}

Output:
Enter the value for radius
5.0
Area of the circle is 78.500003

Scanf(): For user input

In above c program we are asking user to enter the values for variable a and b. You can know more about scanf() method/function in this video tutorial: Using Scanf in C Program

Calculate Area of a Circle without using math.h library: C


[youtube https://www.youtube.com/watch?v=9-_G0N4HnLc]

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


Whenever we declare and initialize a constant, its a convention to make use of capital letter variable. That way we can easily spot and identify a constant in our program. And as we already know, value of constant can not be changed through out the program execution.
Ex: const float PI = 3.14;

Formula for calculating Area of a circle


area of circle

Formula for calculating Area of a circle is PI * radius * radius. We take value of radius from user and using above formula calculate area of the circle and output result to the console window.

Note that we’ve not used math.h library and any of its methods/functions in our program.

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