# C++ function right triangle using two sides

Page 1 of 1

## 5 Replies - 9719 Views - Last Post: 14 November 2012 - 07:42 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=122320&amp;s=f117c2712f6d1b4bd2e623270e8a74ae&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 gummybear

Reputation: 0
• Posts: 1
• Joined: 26-August 09

# C++ function right triangle using two sides

Posted 26 August 2009 - 05:47 AM

write a function named right triangle ( ) that accepts the lengths of two sides of a right triangle as the parameters a and b respectively determine the hypotenuse C, and use pythagorean theory
```#include <iostream>
using namespace std;

void char  RTriangle ( a, b );
int a, b,c, side1, side 2;

int main ( )
{
cin >>side1;
cout <<"Please enter side 2:" <<endl;
cin >> side2;
c^2= a^2 + b^2.
{
cout<<"The Hypotenuse of of the right triangle is:"<<c^2= a^2 +  b^2.<<endl;
}

return 0;

}
```

This post has been edited by jjsaw5: 26 August 2009 - 06:04 AM

Is This A Good Question/Topic? 0

## Replies To: C++ function right triangle using two sides

### #2 jjsaw5

• D.I.C Lover

Reputation: 92
• Posts: 3,063
• Joined: 04-January 08

## Re: C++ function right triangle using two sides

Posted 26 August 2009 - 06:03 AM

What seems to be your issue?

### #3 DaneAU

• Great::Southern::Land

Reputation: 286
• Posts: 1,620
• Joined: 15-May 08

## Re: C++ function right triangle using two sides

Posted 26 August 2009 - 06:29 AM

You need a function that will return the square root of a^2 + b^2 - which in turn will return the length of the hypotenuse.

Eg
```double rTriangle (double a,  double b )
{
double hypotenuse;

// do the maths here using a & b then return it

return hypotenuse;
}

```

This post has been edited by bbq: 26 August 2009 - 06:29 AM

### #4 Cherish.r

Reputation: 0
• Posts: 2
• Joined: 14-November 12

## Re: C++ function right triangle using two sides

Posted 14 November 2012 - 07:11 PM

Hey it didnt work

#include <iostream>
using namespace std;

void double RTriangle ( double a , double b );
int a, b, c, side1, side2;

int main ( )
{
cout<< "PLease enter the length of side1:" << endl;
cin >> side1;
cout <<"Please enter the length of side 2:" << endl;
cin >> side2;
c^2= a^2 + b^2

}
{
cout<<"The Hypotenuse of of the right triangle is:"<<c^2= a^2 + b^2.<<endl;
}

return 0;

}

### #5 #define

• Duke of Err

Reputation: 1853
• Posts: 6,671
• Joined: 19-February 09

## Re: C++ function right triangle using two sides

Posted 14 November 2012 - 07:39 PM

This is not proper code.

```c^2= a^2 + b^2

```

The pow function can be used to calculate powers, but a*a is just as easy.

```hypotenuse_squared = (a * a) + (b * B)/>;

```

The sqrt function can be used to calculate the square root.

```hypotenuse = sqrt(hypotenuse_squared);

```

The sqrt and pow function are in the C++ cmath header.

cmath

• D.I.C Lover

Reputation: 331
• Posts: 1,168
• Joined: 01-April 11

## Re: C++ function right triangle using two sides

Posted 14 November 2012 - 07:42 PM

Cherish.r: Always always, put [ code] tags around any code you post. The icon is in the editor near the center of the top icon bar.

Without those, your code becomes almost impossible to study.

This:
```c^2= a^2 + b^2.

```

is OK for math, but in C you need to either use pow() (part of your compiler's math header file, which you must include), or multiply it yourself.

This post has been edited by Adak: 14 November 2012 - 07:45 PM