C Program To Calculate Area of a Square using its Diagonal

Lets write a C program to find area of a square by using length of its diagonal. We ask the user to input the length of its diagonal.

Related Read:
C Program To Calculate Area of a Square using its Side

Formula To Calculate Area of Square using its diagonal

area = ( diagonal x diagonal ) / 2.0
OR
area = ( diagonal x diagonal ) * 0.5

Note: All the sides of a square are equal. Both the diagonals of the square are of equal length.

area of square using its diagonal

Expected Output for the Input

User Input:
Enter length of diagonal of a Square
15.2

Output:
Area of the Square is 115.52

Video Tutorial: C Program To Calculate Area of a Square using its Diagonal


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

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

Source Code: C Program To Calculate Area of a Square using its Diagonal

#include < stdio.h >

int main()
{
    float area, diagonal;

    printf("Enter length of diagonal of a Square\n");
    scanf("%f", &diagonal);

    area = (diagonal * diagonal) * 0.5;

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

    return 0;
}

Output 1:
Enter length of diagonal of a Square
9
Area of the Square is 40.50

Output 2:
Enter length of diagonal of a Square
5
Area of the Square is 12.50

Output 3:
Enter length of diagonal of a Square
10
Area of the Square is 50.00

Output 4:
Enter length of diagonal of a Square
10.5
Area of the Square is 55.13

Output 5:
Enter length of diagonal of a Square
12.2
Area of the Square is 74.42

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 Calculate Area of a Square using its Side

Lets write a C program to find area of a square by using length of its side. We ask the user to input the length of its side.

Formula To Calculate Area of Square using its side

area = side x side.

Note: All the sides of a square are equal.

Expected Output for the Input

User Input:
Enter the length of side of a square
5

Output:
Area of the Square is 25.00

Video Tutorial: C Program To Calculate Area of a Square using its Side


[youtube https://www.youtube.com/watch?v=V-nCBIaMmYQ]

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

Source Code: C Program To Calculate Area of a Square using its Side

#include < stdio.h >

int main()
{
    float area, side;

    printf("Enter the length of side of a square\n");
    scanf("%f", &side);

    area = side * side;

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

    return 0;
}

Output 1:
Enter the length of side of a square
3
Area of the Square is 9.00

Output 2:
Enter the length of side of a square
4
Area of the Square is 16.00

Output 3:
Enter the length of side of a square
5
Area of the Square is 25.00

Output 4:
Enter the length of side of a square
6
Area of the Square is 36.00

Output 5:
Enter the length of side of a square
8
Area of the Square is 64.00

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 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 Area of an Equilateral Triangle: C Program

Given the sides of the Triangle, write a C program to calculate Perimeter and Area of a equilateral Triangle.

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

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;

So we ask the user to input value of the side only once.

Formula To Calculate Perimeter and Area of Equilateral Triangle

Perimeter = a + a + a;
i.e., Perimeter = 3 * a;

Area = ( ( sqrt(3) / 4 ) * pow(a, 2));

where a is the value of side of the Equilateral Triangle.

Find Area of an Equilateral Triangle: C Program


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

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


Find Area of an Equilateral Triangle: C Program

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

int main()
{
    float sides, area, p;

    printf("Enter value for side of Equilateral Triangle\n");
    scanf("%f", &sides);

    p    = 3 * sides;
    area = ( (sqrt(3)/4) * pow(sides, 2) );

    printf("Perimeter is %f\n", p);
    printf("Area is %f\n", area);

    return 0;
}

Output:
Enter value for side of Equilateral Triangle
75
Perimeter is 225.000000
Area is 2435.696533

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