C Program to Find Perimeter of Triangle

In this article, we are going to write a simple C program, in which we need to calculate the perimeter of the triangle. Calculating the perimeter of a triangle is very simple, through a C program.

The perimeter of a triangle can be simply understood as the sum of all three sides of the triangle. In the program, we will also try to add a validation layer, by checking if the triangle is valid or not. We will calculate the perimeter if and only if the triangle is valid.

C Program to find Perimeter of Triangle

In the given video, you can find a detailed explanation, along with the C program to calculate the perimeter of a triangle, with given sides.

Here is the C program to find the perimeter of the triangle –

#include <stdio.h>

int main() {
double side1, side2, side3, perimeter;

// Prompt the user to enter the lengths of the three sides of the triangle
printf(“Enter the lengths of three sides of the triangle:n”);
scanf(“%lf %lf %lf”, &side1, &side2, &side3);

// Checking if the triangle is valid
if ((side1 < side2 + side3) && (side2 < side1 + side3) && (side3 < side1 + side2)) {

// Calculate the perimeter
perimeter = side1 + side2 + side3;
printf(“The perimeter of the triangle is %.2lfn”, perimeter);
}
else {
printf(“The input does not form a valid triangle.n”);
}

return 0;
}

Program Output 1 –

Enter the lengths of three sides of the triangle:
4 6 8
The perimeter of the triangle is 18.00

Program Output 2 –

Enter the lengths of three sides of the triangle:
12 6 2
The input does not form a valid triangle.