172k views
0 votes
Please write the following task in simple FORTRAN programming language( don't make it complicated).

I need you to make two functions
1 . Area: It takes two real number parameters: length and width and returns length*width.
2. Fac: It is a recursive function. It calculates the n! as n*(n-1)*(n-2)*...*3*2*1. By definition, 0! = 1.
Now, the "main" subprogram, asks the user to enter the length and width of a rectangle, then display "The area is " and display the area using the area( ) function.
Then, ask the user to enter a non-negative integer n, and display n! by calling the fac function.
please submit the whole screenshot and code for this whole program.

1 Answer

1 vote

Final answer:

Fortran functions for calculating area of a rectangle and a factorial are written with the main program handling input and output.

Step-by-step explanation:

In Fortran, we can define the area of a rectangle given its length and width, and also calculate the factorial of a non-negative integer using recursion. The area and fac functions are quite straightforward to implement. Here's an example of how one might write these functions in Fortran:

function area(length, width) result(area_val)
real, intent(in) :: length, width
real :: area_val
area_val = length * width
end function area

recursive function fac(n) result(fac_val)
integer, intent(in) :: n
integer :: fac_val
if (n <= 1) then
fac_val = 1
else
fac_val = n * fac(n-1)
endif
end function fac

program main
real :: length, width
integer :: n
print *, 'Enter the length and width of the rectangle: '
read *, length, width
print *, 'The area is ', area(length, width)
print *, 'Enter a non-negative integer n for factorial calculation: '
read *, n
print *, n, '! = ', fac(n)
end program main


The main program asks for the user inputs, and then displays the results using the defined functions.

User Azodium
by
8.3k points