Practical 3

Posted: January 20, 2012 in Lab 3

Problem Statement-
Write a program that reads in the lengths of three lines and determines whether the three lines can form a triangle. In order for the lines to form a triangle, the length of the longest side must be less than the sum of the lengths of the other two sides.
If a triangle cannot be formed a message indicating this must be displayed. If the lines can form a triangle, a message classifying the triangle as one of the following must be displayed:
• An equilateral triangle – the length of all three sides are equal.
• An isosceles triangle – two sides are equal.
• A scalene triangle – none of the sides are equal.
You should take a modular approach to your program. Your program must include at least the following method:
• A method to display the heading.
• A method that takes three double values as input and returns the maximum of the three
values.
• A boolean method that returns a value of true if the three lines can form a triangle and a value of false otherwise.
The output of your program should be the same as that illustrated in the following examples.
Example 1:
*****************************************
* TRIANGLE CLASSIFICATION *
*****************************************
Enter the length of first line: 5.6
Enter the length of the second line: 2.3
Enter the length of the third line: 1.1
A triangle cannot be drawn from the given lines.
Example 2:
*****************************************
* TRIANGLE CLASSIFICATION *
*****************************************
Enter the length of first line: 7
Enter the length of the second line: 4.5
Enter the length of the third line: 5
The triangle is scalene.

#include<stdio.h>
void heading(void);
float largest(float, float, float);
int istriangle(float, float, float);
void heading(void)
{
	printf("************************************\n");
	printf("* Triangle Classification *   \n");
	printf("*************************************\n");
}
float largest(float a, float b, float c)
{
	if(a>b&&a>c)
		return a;
	else
	if(b>a&b>c)
		return b;
	else
	if(c>a&&c>b)
		return c;
}
int istriangle(float l, float b, float c)
{
	if((b+c)>l)
		return 1;
	else
		return 0;
}
int main()
{
	float a,b,c,l;
	heading();
	printf("Enter the length of first line: ");
	scanf("%f",&a);
	printf("Enter the length of second line: ");
	scanf("%f",&b);
	printf("Enter the length of third line: ");
	scanf("%f",&c);
	l=largest(a,b,c);
	if(l==a)
	if(istriangle(l,b,c))
		{
			if(a==b&&b==c)
				printf("An Equilateral triangle\n");
			else
			if((a==b&&b!=c)||(b==c&&c!=a)||(c==a&&a!=b))
				printf("An isosceles triangle\n");
			else
				printf("Scalene triangle\n");
		}
	else
			printf("A triangle cannot be drawn from the given lines.\n");
	else
	if(l==b)
if(istriangle(l,a,c))
		{
			if(a==b&&b==c)
				printf("An Equilateral triangle\n");
			if((a==b&&b!=c)||(b==c&&c!=a)||(c==a&&a!=b))
				printf("An isosceles triangle\n");
			else
				printf("Scalene triangle\n");
		}
	else
			printf("A triangle cannot be drawn from the given lines.\n");
	else
if(l==c)
if(istriangle(l,a,b))
		{
			if(a==b&&b==c)
				printf("An Equilateral triangle\n");
			if((a==b&&b!=c)||(b==c&&c!=a)||(c==a&&a!=b))
				printf("An isosceles triangle\n");
			else
				printf("Scalene triangle\n");
		}
	else
			printf("A triangle cannot be drawn from the given lines.\n");
return 0;
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s