C Program To Calculate Perimeter, Diagonal of a Square using its Side

Lets write a C program to calculate area, perimeter and diagonal of a square by using length of its side. We ask the user to input the length of its side.

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

Formula To Calculate Area, Perimeter and Diagonal of a Square using its side

diagonal = sqrt(2) x side;

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

Perimeter = 4 x side;

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

Expected Output for the Input

User Input:
Enter length of side of the Square
10.5

Output:
Area of the Square is 110.250000
Perimeter of the Square is 42.000000
Diagonal of the Square is 14.849242

Video Tutorial: C Program To Calculate Perimeter, Diagonal of a Square using its Side



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

Source Code: C Program To Calculate Perimeter, Diagonal of a Square using its Side

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

int main()
{
    float perimeter, diagonal, side, area;

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

    perimeter = 4 * side;
    diagonal  = sqrt(2) * side;
    area      = side * side;

    printf("Area of the Square is %f\n", area);
    printf("Perimeter of the Square is %f\n", perimeter);
    printf("Diagonal of the Square is %f\n", diagonal);

    return 0;
}

Output 1:
Enter length of side of the Square
10
Area of the Square is 100.000000
Perimeter of the Square is 40.000000
Diagonal of the Square is 14.142136

Output 2:
Enter length of side of the Square
5
Area of the Square is 25.000000
Perimeter of the Square is 20.000000
Diagonal of the Square is 7.071068

Output 3:
Enter length of side of the Square
12.2
Area of the Square is 148.839996
Perimeter of the Square is 48.799999
Diagonal of the Square is 17.253405

Output 4:
Enter length of side of the Square
10.5
Area of the Square is 110.250000
Perimeter of the Square is 42.000000
Diagonal of the Square is 14.849242

Output 5:
Enter length of side of the Square
3.2
Area of the Square is 10.240001
Perimeter of the Square is 12.800000
Diagonal of the Square is 4.525484

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