Fortran: How to work out if 3 variables make a right angled triangle

In summary, the conversation discusses a program that sorts three inputted variables and then checks if they can form the sides of a right angled triangle using the equation a^2 + b^2 = c^2. The code provided to attempt this task had a syntax error, which was resolved by changing "=" to "==". The difference between these two operators is that "=" is used for assignment while "==" compares two expressions for equality. This operator was likely borrowed from C-based languages in newer versions of Fortran.
  • #1
Daniel1992
22
0

Homework Statement



I have a program which will sort out three inputted variables into order from lowest to highest. I now must get the program to work out if these 3 values could be the sides of a right angled triangle.


Homework Equations


I am going to use a^2+b^2=c^2 to solve this.

The Attempt at a Solution



Here is the code I used to attempt to solve the problem but I get a syntax error when I try to compile the code.

If (a**2+b**2=c**2) THEN
PRINT*,"These values can make the sides of a right angled triangle"
ELSE
PRINT*,"These values can not make the sides of a right angled triangle"
ENDIF

Any help would be appreciated :)
 
Physics news on Phys.org
  • #2
"=" versus "==" comes to mind. What was the syntax error message?
 
  • #3
lewando said:
"=" versus "==" comes to mind. What was the syntax error message?

Changing "=" to "==" fixed it. Why is that?

By the way thanks for that :approve:
 
  • #4
Daniel1992 said:
Changing "=" to "==" fixed it. Why is that?
Because = and == (or .eq.) are different operators.

= is used only in assignment statements.

== (or .eq.) compare two expressions for equality and is often used in IF blocks.

BTW, it seems to me that the Fortran designers took == from the C-based languages. This operator is relatively new in Fortran.
 
  • #5


Your approach using the Pythagorean theorem is correct. However, there are a few syntax errors in your code that need to be fixed:

1. In Fortran, the exponent operator is "^", not "**". So your code should be:

If (a^2+b^2=c^2) THEN
PRINT*,"These values can make the sides of a right angled triangle"
ELSE
PRINT*,"These values can not make the sides of a right angled triangle"
ENDIF

2. You need to use the "==" operator to check for equality, not the "=" operator. So your code should be:

If (a^2+b^2==c^2) THEN
PRINT*,"These values can make the sides of a right angled triangle"
ELSE
PRINT*,"These values can not make the sides of a right angled triangle"
ENDIF

3. You also need to declare the variables "a", "b", and "c" before using them in the "IF" statement. For example:

REAL :: a, b, c

Once you fix these errors, your code should work as intended.
 

1. How do I determine if three variables form a right angled triangle?

To determine if three variables, representing the sides of a triangle, form a right angled triangle, you can use the Pythagorean theorem. This theorem states that in a right angled triangle, the square of the length of the hypotenuse (the side opposite the right angle) is equal to the sum of the squares of the other two sides. Therefore, you can square each side and check if the sum of the squares of the two smaller sides is equal to the square of the longest side. If it is, then the three variables form a right angled triangle.

2. What are the necessary conditions for a triangle to be right angled?

For a triangle to be right angled, it must satisfy the Pythagorean theorem. In addition, one of the angles in the triangle must be a right angle, which is equal to 90 degrees. If a triangle satisfies these two conditions, then it is a right angled triangle.

3. Can I use Fortran to determine if three variables form a right angled triangle?

Yes, Fortran is a programming language that can be used to solve mathematical problems, including determining if three variables form a right angled triangle. You can write a program that takes in the three variables as input and uses the Pythagorean theorem to check if they form a right angled triangle.

4. What is the Pythagorean triple rule?

The Pythagorean triple rule states that if three positive integers a, b, and c satisfy the equation a^2 + b^2 = c^2, then they form a Pythagorean triple. This means that the three numbers can represent the sides of a right angled triangle, with c being the length of the hypotenuse. This rule can be used to determine if three variables form a right angled triangle.

5. Are there any other methods to determine if three variables form a right angled triangle?

Yes, there are other methods to determine if three variables form a right angled triangle, such as using trigonometric ratios. However, the Pythagorean theorem is the most commonly used method as it is simple and straightforward. Other methods may be more complex and require more calculations.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Precalculus Mathematics Homework Help
Replies
9
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
Replies
2
Views
788
  • Engineering and Comp Sci Homework Help
Replies
23
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Precalculus Mathematics Homework Help
Replies
7
Views
1K
Back
Top