program triangle implicit none real :: x,y ! the two known sides of the triangle real :: alpha ! the angle enclosed by the two known sides of the triangle real :: z,area ! the third side and the area of the triangle (these are functions) real :: pi ! constant integer :: again ! whether to perform another operation pi=3.1415926 print *,'' ! skip a line print *,'Given the lengths x and y of two sides of a triangle,' print *,'and the angle alpha contained by these two sides' print *,'this program calculates the length of the third side z' print *,'and the area of the triangle' print *,'' do do ! Ask user for values: print *,'Enter the value of x' read *,x print *,'Enter the value of y' read *,y print *,'Enter the value of alpha in degrees' read *,alpha ! Check that they are sensible: if(x>0. .and. y>0. .and. alpha>0. .and. alpha<180.) exit ! If they are not print a warning: print *,'WARNING: incorrect values entered, please re-enter the values' end do ! Convert alpha to radians alpha=alpha*pi/180. print *,'' print '("The length of the third side is ",F10.5)',z(x,y,alpha) print '("The area of the triangle is ",F10.5)',area(x,y,alpha) print *,'' ! Check whether the user wishes to try again do print *,'Do you wish to try again? enter 1 for yes and 0 for no' read *,again ! Check if the value is correct if(again==1 .or. again==0) exit ! Otherwise print warning print *,'WARNING: please enter 1 or 0' end do print *,'' ! If again==0 then end the program if(again==0) exit end do print *,'Program ended - Bye!' print *,'' end program triangle real function z(side1,side2,angle) implicit none real,intent(in) :: side1,side2,angle ! the two sides and the enclosed angle ! calculate z: z=sqrt(side1**2.+side2**2.-2.*side1*side2*cos(angle)) end function z real function area(side1,side2,angle) implicit none real,intent(in) :: side1,side2,angle ! the two sides and the enclosed angle ! calculate the area: area=0.5*side1*side2*sin(angle) end function area