Fortran Problems Set 2
Created
September 24, 2012 02:10
-
-
Save matiskay/3773833 to your computer and use it in GitHub Desktop.
Fotran Problems set 2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
! gfortran 1.f90 | |
program main | |
implicit none | |
integer :: a | |
integer :: n | |
print *, 'Insert the value of a: ' | |
read (*, *) a | |
n = 7 | |
if (mod(a, n) == 0) then | |
print *, a, 'is divisible by ', n | |
else | |
print *, a, 'is not divisible by ', n | |
end if | |
end program main |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
! gfortran 1.f90 | |
program main | |
implicit none | |
real :: INTEREST = 0.06 | |
real :: capital | |
real, parameter :: K = 2000.0 | |
integer, parameter :: Y0 = 1992 | |
integer, parameter :: YF = 2002 | |
integer :: delta | |
integer :: i | |
do while (Y0 + i <= YF) | |
print *, Y0 + i, capital(K, INTEREST, i) | |
i = i + 1 | |
end do | |
end program main | |
real function capital(k, interest, n) | |
implicit none | |
real :: k | |
real :: interest | |
integer :: n | |
capital = ((1 + interest) ** n ) * k | |
return | |
end function capital |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
! gfortran file-name.f90 | |
program main | |
implicit none | |
integer, parameter :: MILLON = 10 ** 6 | |
integer, parameter :: POP_1980 = 120 * MILLON | |
integer, parameter :: INITIAL_YEAR = 1980 | |
integer, parameter :: POP_TARGET = 200 * MILLON | |
real :: PERCENTAGE = 5.0 / 100.0 | |
integer :: current_pop | |
integer :: year | |
real :: increase_by | |
! print *, POP_1980 | |
year = INITIAL_YEAR | |
current_pop = POP_1980 | |
do while (current_pop <= POP_TARGET) | |
current_pop = increase_by(current_pop, PERCENTAGE) | |
year = year + 1 | |
end do | |
print *, 'The population is more than 200 millions in the year ', year | |
print *, 'Population: ', current_pop | |
end program main | |
real function increase_by(pop, percentage) | |
implicit none | |
integer :: pop | |
real :: percentage | |
percentage = percentage | |
increase_by = pop + percentage * pop | |
return | |
end function increase_by |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
! gfortran file-name.f90 | |
! MAXVAL(ARRAY, dim, mask) returns the largest value in the array ARRAY, of those that obey the relation in the third argument MASK if that one is given, along only the desired dimension if the second argument DIM is given. | |
! MINVAL(ARRAY, dim, mask) returns the smallest value in the array ARRAY, of those that obey the relation in the third argument MASK if that one is given, along only the desired dimension if the second argument DIM is given. | |
program main | |
implicit none | |
integer :: io_status | |
integer :: i | |
integer, parameter :: SIZE=8 | |
real :: list(SIZE) | |
real :: elem | |
open(unit=56, file='datos.dat') | |
i = 1 | |
do | |
READ(56, *, iostat=io_status) elem | |
! Not error occurs | |
if (io_status > 0) then | |
print *, 'ERROR' | |
stop | |
!exit | |
else if (io_status < 0) then | |
! print *, 'EOF' | |
exit | |
else | |
! Operation | |
!print *, elem | |
list(i) = elem | |
end if | |
i = i + 1 | |
end do | |
print *, 'Max Value: ', maxval(list, i) | |
print *, 'Min Value: ', minval(list, i) | |
end program main |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
! gfortran file-name.f90 | |
program main | |
implicit none | |
real :: equation | |
real :: i | |
i = -4 | |
do while (i <= 4) | |
write(*,*) 'i =', i, 'value = ', equation(i) | |
i = i + 0.2 | |
end do | |
end program main | |
real function equation(x) | |
implicit none | |
real :: x | |
equation = x ** 4 - 5 * (x ** 2) + 7 * x - 8 | |
return | |
end function equation |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3.45 | |
5.23 | |
1.26 | |
7.34 | |
5.21 | |
4.34 | |
3.41 | |
6.34 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
! gfortran file-name.f90 | |
program main | |
implicit none | |
end program main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment