w3resource

C Exercises: Calculate the perimeter of the triangle

C Basic Declarations and Expressions: Exercise-23 with Solution

Write a C program that reads three floating values and check if it is possible to make a triangle with them. Also calculate the perimeter of the triangle if the said values are valid.

Pictorial Presentation:

C Programming: Calculate the perimeter of the triangle

C Code:

#include <stdio.h>
int main() {
	float  x, y, z, P, A;
    printf("\nInput the first number: "); 
    scanf("%f", &x);
    printf("\nInput the second number: ");
    scanf("%f", &y);
    printf("\nInput the third number: ");
    scanf("%f", &z);

    if(x < (y+z) && y < (x+z) && z < (y+x)) 
     {  
	 P = x+y+z;
	 printf("\nPerimeter  = %.1f\n", P);	 
	 
    }
    else
    {
    	printf("Not possible to create a triangle..!");
	}
}

Sample Output:

Input the first number: 25                                             
                                                                       
Input the second number: 15                                            
                                                                       
Input the third number: 35                                             
                                                                       
Perimeter  = 75.0

Flowchart:

C Programming Flowchart: Calculate the perimeter of the triangle

C Programming Code Editor:


Contribute your code and comments through Disqus.

Previous: Write a C program that read 5 numbers and sum of all odd values between them.
Next: Write a C program that reads two integers and checks if they are multiplied or not.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Share this Tutorial / Exercise on : Facebook and Twitter

C Programming: Tips of the Day

How to increment a pointer address and pointer's value?

First, the ++ operator takes precedence over the * operator, and the () operators take precedence over everything else.

Second, the ++number operator is the same as the number++ operator if you're not assigning them to anything. The difference is number++ returns number and then increments number, and ++number increments first and then returns it.

Third, by increasing the value of a pointer, you're incrementing it by the sizeof its contents, that is you're incrementing it as if you were iterating in an array.

Ref : https://bit.ly/3vMewPt