C program To Find Area of Right Angled Triangle

Lets write a C program to calculate area of a right angled Triangle, by asking the user to enter its width and height.

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

Formula To Find Area of Right Angled Triangle

Area = (width * Height) / 2.0;

OR

Area = (width * Height * 0.5);

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).

C program To Find Area of Right Angled Triangle


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

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


Source Code: C program To Find Area of Right Angled Triangle

#include < stdio.h >

int main()
{
    float h, w, area;

    printf("Enter height and width of a right angled triangle\n");
    scanf("%f%f", &h, &w);

    area = (h * w) / 2.0;

    printf("Area of a Right Angled Triangle is %f\n", area);

    return 0;
}

Output:
Enter height and width of a right angled triangle
10
5
Area of a Right Angled Triangle is 25.000000

Validate the Input and Find Area of Triangle: C Program

#include < stdio.h >

int main()
{
    float h, w, area;

    printf("Enter height and width of a right angled triangle\n");
    scanf("%f%f", &h, &w);

    if(w == 0 || h == 0)
    {
        printf("Invalid Input\n");
    }
    else
    {
        area = (h * w) / 2.0;
        printf("Area of a Right Angled Triangle is %f\n", area);
    }

    return 0;
}

Output 1:
Enter height and width of a right angled triangle
10
5
Area of a Right Angled Triangle is 25.000000

Output 2:
Enter height and width of a right angled triangle
0
5
Invalid Input

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 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

Find Missing Angle in a Triangle if two angles are given: C Program

We ask the user to input 2 angles of the Triangle. And using C Program we calculate and output the 3rd angle.

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

Formula To Calculate Valid Triangle

a + b + c = 180;

Formula To Find Missing Angle of Triangle

c = 180 – (a + b);

where a, b and c are angles of Triangle.

Note: Also not that if any of the angle is 0, then those 3 angles can’t form a Triangle, so it’s a invalid Triangle.

Find Missing Angle in a Triangle if two angles are given: C Program


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

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


Find Missing Angle in a Triangle if two angles are given: C Program

#include < stdio.h >

int main()
{
    int a1, a2, a3;

    printf("Enter 2 angles of Triangle\n");
    scanf("%d%d", &a1, &a2);

    a3 = 180 - ( a1 + a2 );

    if(a1 != 0 && a2 != 0 && a3 != 0)
    {
        printf("The 3rd angle is %d\n", a3);
    }
    else
    {
        printf("Invalid angles of Triangle\n");
    }

    return 0;
}

Output 1:
Enter 2 angles of Triangle
0
90
Invalid angles of Triangle

Output 2:
Enter 2 angles of Triangle
90
90
Invalid angles of Triangle

Output 3:
Enter 2 angles of Triangle
90
80
The 3rd angle is 10

Output 3 outputs valid Triangle as addition of all 3 angles gives 180.
90 + 80 + 10 = 180.

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 Angles: C Program

Three angles 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 all the angles is exactly equal to 180.

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

Logic To Find Valid Triangle or Not

We ask the user to enter all 3 angles of a Triangle. Then we add all these angles and if the result is 180 then its a valid Triangle, if not, its not a valid Triangle.

Formula To Calculate Valid Triangle
a + b + c = 180;

where a, b and c are 3 angles of a Triangle.

Note: Also not that if any of the angle is 0, then those 3 angles can’t form a Triangle, so it’s a invalid Triangle.

Triangle Valid or Not based On Angles: C Program


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

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


Triangle Valid or Not based On Angles: C Program

#include < stdio.h >

int main()
{
    int a1, a2, a3, sum;

    printf("Enter 3 angles of a Triangle\n");
    scanf("%d%d%d", &a1, &a2, &a3);

    sum = a1 + a2 + a3;

    if(sum == 180 && a1 != 0 && a2 != 0 && a3 != 0)
    {
        printf("It's a valid Triangle\n");
    }
    else
    {
        printf("It's not a valid Triangle\n");
    }

    return 0;
}

Output 1:
Enter 3 angles of a Triangle
90
80
0
It’s not a valid Triangle

Output 2:
Enter 3 angles of a Triangle
90
90
90
It’s not a valid Triangle

Output 3:
Enter 3 angles of a Triangle
90
80
10
It’s a valid Triangle

Output 3 outputs valid Triangle as addition of all 3 angles gives 180.
90 + 80 + 10 = 180.

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

C Program To Check whether a Triangle is Equilateral, Isosceles or Scalene

If the three sides of a Triangle are entered through the keyboard, write a program to check whether the Triangle is isosceles, equilateral, scalene Triangle.

Note:
Equilateral Triangle: A Triangle is called equilateral triangle if length of 3 sides of it are equal.
Example: a = 10, b = 10, c = 10;

Isosceles Triangle: A Triangle is called isosceles triangle if length of 2 sides of it are equal.
Example: a = 5, b = 10, c = 10;

Scalene Triangle: A Triangle is called scalene triangle if length of all 3 sides are different or not equal.
Example: a = 5, b = 6, c = 10;

C Program To Check whether a Triangle is Equilateral, Isosceles or Scalene

#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 && b == c)
        {
            printf("It's an Equilateral Triangle\n");
        }
        else if(a == b || a == c || b == c)
        {
            printf("It's an Isosceles Triangle\n");
        }
        else
        {
            printf("It's a Scalene Triangle\n");
        }

    return 0;
}

Output 1:
Enter values for a, b and c
10
10
10
It’s an Equilateral Triangle

Output 2:
Enter values for a, b and c
20
20
30
It’s an Isosceles Triangle

Output 3:
Enter values for a, b and c
10
15
8
It’s a Scalene Triangle

Here a, b and c are lengths of sides of the triangle. First we check if length of a is equal to length of b and length of c. If that’s true, then its Equilateral Triangle.

Next, if the first condition is false – we check if either length of a is equal to length of b or length of a is equal to length of c or length of b is equal to length of c. If any of these conditions are true, then 2 sides of the Triangle are equal. So its an Isosceles Triangle.

If above two conditions are false, then length of all sides of the Triangle is different or not equal, so it’s a Scalene Triangle.

C Program To Check whether a Triangle is Equilateral, Isosceles or Scalene


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

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


Check If Triangle is valid or not

First we ask the user to input lengths of 3 sides of the Triangle. We’ll need to write the C program to check if the entered values form a valid Triangle or not. You can find the logic to it at Triangle Valid or Not based On Sides: C Program.

C Program To Check whether a Triangle is Equilateral, Isosceles or Scalene: Valid Triangle

#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)
    {
        if(a == b && b == c)
        {
            printf("It's an Equilateral Triangle\n");
        }
        else if(a == b || a == c || b == c)
        {
            printf("It's an Isosceles Triangle\n");
        }
        else
        {
            printf("It's a Scalene Triangle\n");
        }
    }
    else
    {
        printf("Invalid Triangle\n");
    }

    return 0;
}

Output 1:
Enter values for a, b and c
4
5
6
It’s a Scalene Triangle

Output 2:
Enter values for a, b and c
30
30
30
It’s an Equilateral Triangle

Output 3:
Enter values for a, b and c
10
50
50
It’s an Isosceles Triangle

Output 4:
Enter values for a, b and c
10
50
20
Invalid Triangle

Note:
Result of output 4 gives “Invalid Triangle” because – the largest side is 50. So the sum of other two sides must be greater than 50. (10+20) = 30. So 30 is not equal to 50, so Triangle can’t be formed with length of sides as 10, 50 and 20.

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