In: Computer Science
Write in c++ as simple as possible
In a right triangle, the square of the length on one side is equal to the sum of the squares of the lengths of the other two sides. Write a program that prompts the user to enter the length of three sides of the tringle (as doubles) and the outputs a message indication whether the triangle is a right triangle. You should split this into two functions (but only one .cpp file). One function is main() where you input the values of the three sides, and then print out the final answer. The other function is where you determine if the triangle is a right triangle. Watch out about using == with doubles. Put comments at the top of your code.
/*Write a program to check whether triangle is right angled
triangle or not. */
#include<iostream.h>
#include<conio.h>
int findRightTriangle(double,double,double);
void main()
{
double a,b,c;
int ch=0;
clrscr();
cout<<"Enter first side of triangle: ";
cin>>a; //accepts one side of triangle from user
cout<<"Enter second side of triangle: ";
cin>>b;
cout<<"Enter third side of triangle: ";
cin>>c;
ch=findRightTriangle(a,b,c); //calls findRightTriangle
function
if(ch==1)
cout<<"Triangle is right triangle"<<endl;
else
cout<<"Triangle is not right triangle"<<endl;
getch();
}
int findRightTriangle(double a,double b,double c)
{
if((a*a+b*b==c*c)||(a*a+c*c==b*b)||(c*c+b*b==a*a)) //pythagoras
theorem
return 1;
else
return 0;
}
Output:
Please give positive rating.