0% found this document useful (0 votes)
92 views21 pages

Fortran 90 Programmes

The document contains 15 FORTRAN programs for numerical methods and calculations: 1) Addition of integers 2) Finding the largest of three numbers 3) Arranging numbers in ascending/descending order using arrays 4) Testing if a number is prime 5) Generating the first 100 prime numbers 6) Matrix addition and multiplication using 2D arrays 7) Calculating the factorial of an integer 8) Generating a Fibonacci series 9) Finding roots using the bisection method 10) Finding roots using Newton-Raphson method 11) Finding roots using the secant method 12) Finding roots using the Regula-Falsi method 13) Performing least squares linear

Uploaded by

sandesh karad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
92 views21 pages

Fortran 90 Programmes

The document contains 15 FORTRAN programs for numerical methods and calculations: 1) Addition of integers 2) Finding the largest of three numbers 3) Arranging numbers in ascending/descending order using arrays 4) Testing if a number is prime 5) Generating the first 100 prime numbers 6) Matrix addition and multiplication using 2D arrays 7) Calculating the factorial of an integer 8) Generating a Fibonacci series 9) Finding roots using the bisection method 10) Finding roots using Newton-Raphson method 11) Finding roots using the secant method 12) Finding roots using the Regula-Falsi method 13) Performing least squares linear

Uploaded by

sandesh karad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 21

INDEX

S.No. Name of the FORTRAN Program


1 Addition of two integers
2 Largest number from the user defined three integers
3 Arrange the numbers in ascending/descending order using 1-D array.
4 Prime number test
5 Generate first 100 prime numbers
6 Matrix addition and multiplication using 2-D array
7 Factorial of an integer number (Extra)
8 Generate a Fibonacci series (extra)
9 Root of a function using Bisection
10 Root of a polynomial function using Newton Raphson method
11 Root of a polynomial function using Secant method
12 The Regula–Falsi Method for estimating the roots of a polynomial f(x)
13 Least square linear-fitting to the given data
14 Numerical integration by Trapezoidal method
15 Numerical integration by Simpson 1/3rd method
Page 1 [C:\fortran\add.f90]
!programme of addition of two integer

Program add implicit


none real :: x, y, z

do
z=x+y
read*,x,y print*,x+y
enddo print*,"addition of two number is",z end
Page 1 [C:\fortran\compare3.f90]
!programe to find the largest among three number

Program largest implicit none

real :: a,b,c
print*,"enter the three number"
read*,a,b,c
if (a>=b) then

if (a>=c) then
print*, "largest number is",a
else print*, "largest number is",c
endif else

if (b>=c) then
print*,"largest number is",b
else print*, "largest number is",c endif endif
end
Page 1 [C:\fortran\ascending_order.f90]
!programe to arrange ascending order of numbers

program ascending_order implicit none


integer::i,n,j,temp,A(100)

print*,"Enter the total numbers of terms" read*,n

print*,"Enter the numbers to be arranged" read*,(A(i),i=1,n)

Do j=1,n
Do i=1,j-1 if (A(i)>A(i+1)) then

temp=A(i) A(i)=A(i+1)
A(i+1)=temp
endif enddo enddo

print*,"Numbers is ascending order are as follow" print*,(A(i),i=1,n) end


program ascending_order
Page 1 [C:\fortran\descending_order.f90]
!programe to arrange descending order of numbers

program descending_order

implicit none

integer::i,n,j,temp,A(100)

print*,"Enter the total numbers of terms" read*,n

print*,"Enter the numbers to be arranged" read*,(A(i),i=1,n)

Do j=n,2,-1
Do i=1,j-1 if (A(i)<A(i+1)) then

temp=A(i+1) A(i+1)=A(i)
A(i)=temp endif
enddo
enddo

print*,"Numbers is descending order are as follow" print*,(A(i),i=1,n) end


program descending_order
Page 1 [C:\fortran\primes.f90]
!programe to find number is prime or not

program primes

implicit none

integer :: Range,Optimusprime

integer :: divisor

Range = 100

do Optimusprime = 3, Range, 2 divisor = 3


do
if(divisor*divisor>Optimusprime .or. mod(Optimusprime,divisor)==0)exit divisor =divisor + 2
enddo

if(divisor*divisor>Optimusprime) then print*,


Optimusprime endif
enddo
end
Page 1 [C:\fortran\100primes.f90]
!programe to find primes upto 100

program primes

implicit none

integer :: Range,prime integer :: divisor

Range = 100

do prime = 3, Range, 2
divisor = 3 do if(divisor*divisor>prime .or. mod(prime,divisor)==0)exit
divisor =divisor + 2
enddo

if(divisor*divisor>prime) then
print*, prime
endif
enddo
end
Page 1 [C:\fortran\matrix_addition.f90]
!programme for matrix addition

program matrix_addition

implicit none

integer :: i,j,m,n integer :: a(5,5),b(5,5),c(5,5)

print*,"enter the no. of rows and columns of matrix A & B" read*,m,n print*,"enter
the elements of matrix A"

do i =1,m
read*, (a(i,j),j=1,n)
enddo print*,"enter the elements of matrix B"

do i=1,m
read*,(b(i,j),j=1,n)
enddo print*,"the sum of two matrix is"

do i=1,m do
j=1,n

c(i,j)=a(i,j)+b(i,j) enddo
enddo

do i=1,m
print*,(c(i,j),j=1,n)
enddo end program
matrix_addition
Page 1 [C:\fortran\matrix_multiplication.f90]
!programe to find matrix multiplication

program matrix_multiplication

implicit none

integer::i,j,m,n,k,l,A(100,100),B(100,100),C(100,100)

print*,"Enter the numbers of row of first matrix"


read*,n
print*,"Enter the numbers of colums of second matrix"
read*,m print*,"enter the elements of first matrix"

Do i=1,n
Do j=1,m read*,A(i,j)

enddo
enddo
print*,"Enter the numbers of colums of second matrix" read*,l print*,"enter the
elements of second matrix"

Do i=1,m
Do j=1,l read*,B(i,j)

enddo
enddo

do i=1,n do
j=1,l c(i,j)=0
do k=1,m c(i,j)=c(i,j)+A(i,k)*b(k,j)
enddo enddo
enddo

do i=1,n print*,(C(i,j),j=1,l)
enddo end
Page 1 [C:\fortran\factorial.f90]
!programme of factorial

program factorial

implicit none

integer :: n,fact integer :: i

print*,"give the no n" read*,n if (n < 0) error stop "factorial is singular for negative integers"

fact = 1.0
do i = 2, n
fact = fact * i
enddo print*,"factorial of n",fact end

program factorial
Page 1 [C:\fortran\fibonacci.f90]
!programme for genarate fibonacci series

program Fibonacci

implicit none

integer :: x,y,temp,ix

x=0 y=1
print*,x print*,y

do ix = 1,45,1
temp=x+y x=y
y=temp
print*,temp
enddo end
program fibonacci
Page 1 [C:\fortran\bisection.f90]
!Bisection fuction

Program bisection_function

implicit none

real :: a,b,r,g,p,error,c,f integer :: i,itr

print*,"enter two guess values and the max iteration on you prefer" read*,a,b print*,a,b

print*,"give maximum error"


read*,error print*,error

itr= 0 p=f(a)*f(b) if(p>0) then print*, "enter another guess

value" else

do
itr=itr+1 c=(a+b)/2
if(f(a)*f(c)<0)then
r=(b-c)/c
g=abs(r) b=c
else

if(f(c)*f(b)<0)then
r=(a-c)/c a=c
endif

if(g<r)then
print*,"c is root"
print*,"value of function at c",f(c) print*,"no. of iteration",i
exit
endif endif enddo
endif
end program bisection_function

!function subprogram
function f(x) real ::
f real :: x
Page 2 [C:\fortran\bisection.f90]

f=2*x-2 end

function f
Page 1 [C:\fortran\newton.f90]

!newton raphson method

program newton_raphson_method

implicit none

real :: x0,x1,x,f,df,e,n,d integer :: p

print*,"give the values of x0 , w" read*,x0,e

do p=1,100 d=df(x0) x1=x0-f(x0)/d if(abs((x1-x0)/x0)<e) then


print*,"the root is x1",x1,"iteration",p print*,"the value of f at root
is",f(x1) stop else x0=x1
endif
enddo
print*,"no root found"
end

function f(X) real :: f


real,intent(in):: x
f=x*x*x+3*x*x-3*x+1
endfunction

function df(X) real :: df


real,intent(in):: x
df=3*x*x+6*x-3 endfunction
Page 1 [C:\fortran\secant.f90]
!secant method

program matrix_addition

implicit none

real :: x0,x1,x2,f,f0,f1,f2,e,tol integer :: i

print*,"give the initial guess values" read*,x0,x1

print*,"give error and tolerance" read*,e,tol

do i=1,100 f0=f(x0)
f1=f(x1)

if(abs(f1-f0)<e) then print*,"slope is too small"


print*,"no root found"
exit endif
x2 = x1-((x1-x0)/(f1-f0))*f1 f2=f(x2)
if(abs((x1-x0)/x1)<tol) then

print*,x2,i,"interation to required to coverage to root" print*,"the value of the


function at the root is",f2 exit else x0=x1 x1=x2 f0=f1 f1=f2
endif
enddo
end

function f(X) real :: f


real,intent(in):: x

f=x*3+3*x*x-3*x+1 endfunction
Page 1 [C:\fortran\falsi.f90]

!Regula–Falsi Method for fuction

program falseposition

implicit none

real:: fa,x,fb,xa,tol,f,fc,fd integer::


m,n,ka,kb tol = 0.001

print*,"Enter number of iterations and bounds of search interval." read*, m,fa,fb

do while (f(fa)*f(fb) .gt. 0)


print*, "Try another pair of boundaries." read*, fa,fb
end do

n=0 ka=0
kb=0 fc = f(fa)
fd = f(fb)

do while (1 .eq. 1) if (n .eq. m)


then print*, "Failed" exit
end if

x=(fa*fd-fb*fc)/(fd-fc) if (f(x) .eq. 0)


then
print *, "The root is",x exit
else if (f(x)*fc .gt. 0) then

fa = x fc =
f(fa) ka = 0 kb
= kb+1
if (kb .ge. 2) then
fd = 0.5*fd
end if

else
fb = x fd =
f(fb) kb = 0 ka
= ka+1
if (ka .ge. 2) then fc =
0.5*fc end if
end if xa = (fa*fd-fb*fc)/(fd-fc) if (abs(f(xa)-

f(x)) .lt. tol) then

print *, "The root is", xa print


*,n,"iterations." exit
Page 2 [C:\fortran\falsi.f90] end if

n = n+1
end do end program

falseposition

real function f(x) real:: x f=3*x**4-


5*x**3+2*x**2-x-1 return end
function f
Page 1 [C:\fortran\least.f90]

!do a least squares fit to a straight line

program least_square

implicit none

integer :: j,i,del,slope
real :: x,y,a,sumx,sumy,sumxy,sumxx,sumyy,c(100),d(100)

print*,"number of data points" read*,j

print*,"data points" read*,c(i),d(i)

open(unit=10,file="data-1.txt")
do i=1,j read(10,*) c(i),d(i)
enddo

sumx =0
sumy =0
sumxy =0
sumxx =0
sumyy=0 do
i=1,j

sumx = sumx + c(i) sumy = sumy +


d(i) sumxy =sumxy + c(i)*d(i) sumxx
=sumxx + c(i)*c(i) sumyy =sumyy
+d(i)*d(i) enddo

del = j*sumxx-sumx*sumx slope = (j*sumxy-


sumx*sumy)/del a = ((sumxx*sumy)-
(sumx*sumxy))/del

print*,slope print*,"intercept",a
end program least_square
Page 1 [C:\fortran\trapozidal.f90]

! TRAPEZOIDAL METHOD FOR NUMERICAL INTEGRATION

program trapezoid

implicit none

real :: f, g, a, b, h, s, x, approxA, exA, e integer :: n, i

print*, "Enter The INTERVAL Range" read*, n

print*, "Enter The INTEGRATION LIMITS" read*, a, b

h = (b - a)/real (n) s = 0.0 do i


= 1, n -1

x = a + (real (i) * h) s = s + f(x)

end do approxA = h*(f(a) + f(b) + 2*s)/2.0 print*, "The APPROXIMATE

Value Of The Integral Is", approxA

exA = g(b) - g(a)

print*, "The EXACT Value Of The Integral Is", exA e = abs ((approxA -

exA))/approxA

print*, "The RELATIVE Error Is", e print*, "The


Interval Length Is", h end program trapezoid

! FUNCTION DECLARATION

real function f(x) real :: x f =

x**5 + x**3

return end function f real function g(x)

real :: x g = (x**6)/6.0 + (x**4)/4.0 return


Page 2 [C:\fortran\trapozidal.f90]

end function g
Page 1 [C:\fortran\simpson.f90]

! SIMPSON'S 1/3 RULE FOR NUMERICAL INTEGRATION

program simpson

implicit none

real :: a, b, x, y, f, g, h, s1, s2, approxA, expA, e integer :: i, n, m, l

print*, "Enter The Number Of Intervals" read*, n

print*, "Enter The Limits Of INTEGRATION" read*, a, b

h = (b - a)/n s1 =
0.0 s2 = 0.0 m =
n/2

do i = 1, m

x = a + (2 * real (i) - 1) * h s1 = s1 + f(x) end

do l = (n - 2)/2

do i = 1, l

y = a + 2 * real(i) * h s2 = s2 + f(y) end do

approxA = (h/3.0)*(f(a) + f(b) + 4*s1 + 2*s2)

print*, "The APPROXIMATE Value Of The INTEGRAL Is", approxA expA = g(b) -
g(a)

print*, "The EXPECTED Value Of The INTEGRAL Is", expA e = abs


((approxA - expA))/approxA

print*, "The RELATIVE Error Is", e print*, "The

Interval Width Is", h

end program simpson

! FUNCTION DECLARATION real function f(x) real ::

x f = x**5 + x**3

return

Page 2 [C:\fortran\simpson.f90]

end function f real function g(x) real :: x g =

(x**6)/6.0 + (x**4)/4.0 return end function

You might also like