Fortran Assignment
Name: Md.Imran Hossain Munnay
Student ID: 2012015
Date of Sub: 11/15/2024
Example (2D Array with Implied Do loop):
[Checking if any particular value is present in the file using logical found]
Code:
program value_present
implicit none
integer :: A(4,4), n, i, j
logical :: found
open(1, file = 'Value list.txt')
read(1,*) A
write(*, '(A)', advance = 'no') 'Enter the value you want to
check ='
read*, n
do i = 1, 4
do j = 1, 4
if (A(i,j) == n) then
found = .true.
end if
end do
end do
if (found) then
print*, 'The value is present in this Array'
else
print*, 'The value is not present!'
end if
stop
end program
Input Data & Output:
Exercise 01:
[Write a Fortran program which calculates the following up to five decimal places: ]
1 1 1 1
........
1 3 5 21
Code:
program inverse_sum
implicit none
integer :: i
real :: sum
sum = 0.0
do i = 1, 21, 2
sum = sum + (1.0/i)
end do
write(*,99) sum
99 format('The sum of the series is = ', f10.5)
stop
end program
Output:
Exercise 02:
[Write a Fortran program that prints the sum of first fifty odd numbers using Do loop]
Code:
program fifty_sum
implicit none
integer :: i, n
real :: sum
write(*,'(A)', advance = 'no')'How many Odd numbers you want to sum (from
first) = '
read*, n
sum = 0.0
do i = 1, 2*n, 2
sum = sum + i
end do
print*, 'The sum of first', n, ' odd number is = ',sum
stop
end program
Output:
Exercise 03:
[Consider the quadratic polynomial y = 2x2 - 3x - 5. Write a Fortran program which finds
y for values of x from - 4 to 4 in steps of 0.5]
Code:
program quadratic_solve
implicit none
integer :: i
real :: x, y, ans(17)
x = -4
do i = 1, 17, 1
y = 2*(x**2) - 3*x - 5
x = x + 0.5
ans(i) = y
print*, y
end do
print*, 'All the reults are : ', ans
stop
end program
Output:
Exercise 04:
[Write a Fortran program to find the smallest of 100 numbers using DO loop]
Code:
program smallest_number
implicit none
real :: X(100), small
integer :: i
open(1, file = 'Input numbers.txt')
open(2, file = 'Output result.txt')
read(1,*) X
small = X(1)
do i = 2, 100
if (X(i) < small) then
small = X(i)
end if
end do
print*, 'The smallest number among them is = ', small
write(2, 99) small
99 format('The smallest number among them is =', f10.3)
stop
end program
Input & Output:
100 random integer (range 0 to 100) numbers are used for input.
Exercise 05:
[Determine the sectional area under a curve which is given in the table below. Distance
between ordinates is 7 m]
Code:
program sectional_area
implicit none
real :: Or(10), SM(10), SM_product, area
integer :: i, h
open(1, file = 'Ordinate input.txt')
open(2, file = 'SM input.txt')
open(3, file = 'Area output.txt')
h = 7
read(1,*) Or
read(2,*) SM
SM_product = 0.0
do i = 1, 10
SM_product = SM_product + SM(i)*Or(i)
end do
area = (3.0/8) * SM_product * h
write(3, '(A, f10.3)') 'The sectional area under the curve is (in sq m) =
', area
stop
end program
Input & Output: