You are on page 1of 14

#3.

1(a)
***********************************************************************************
****************************************
the constant Pie can be approximated by (Pie)^2/6=1+1/4+1/9+1/16+....... Write a
fortran program to find
the sum of the first 1200 terms. Hence find the value of Pie in 8 decimal places.
***********************************************************************************
****************************************
open(1,file='p1.dat')
open(2,file='p2.dat')
read(1,*)n
sum1=0
do i=1,n
sum1=sum1+1/float(i**2)
end do
pi1=sqrt(6*sum1)
write (2,10)pi1
10 format (3x,"sum is=",f10.8)
close(1)
close(2)
stop
end

!!Input!!
...................................................................................
........................................
1200
...................................................................................
........................................

#3.1(b)
***********************************************************************************
****************************************
the constant Pie can be approximated by (Pie)^2/6=1+1/4+1/9+1/16+....... Write a
fortran program to find
the sum of the first 1200 terms in reverse order. Hence find the value of Pie in 8
decimal places.
***********************************************************************************
****************************************
open(1,file='p1.dat')
open(2,file='p2.dat')
read(1,*)n
sum1=0
do i=n,1,-1
sum1=sum1+1/float(i**2)
end do
pi1=sqrt(6*sum1)
write (2,10)pi1
10 format (3x,"reverse sum is=",f10.8)
close(1)
close(2)
stop
end

!!Input!!
...................................................................................
........................................
1200
...................................................................................
........................................

#3.2
***********************************************************************************
****************************************
Given that quadratic polynomial y=2x^2-3x-5. Write a fortran Program to find y for
values of x from -5 to 4 in steps of 0.5.
The output should be in two columns with proper heading.
***********************************************************************************
****************************************
open(2,file='p2.dat')
write (2,10)
10 format("value of 'x'",1x,"value of 'y'")
do x=-5,4,.5
y=2*x**2-3*x-5
write (2,20)x,y
20 format (f5.2,8x,f5.2)
end do
close(2)
stop
end

!!Input!!
...................................................................................
........................................
\*nothing to input.......*\
...................................................................................
........................................

#3.3
***********************************************************************************
****************************************
A class of 10 students takes an exam on which scores from 0 to 100. write a fortran
program which find
(a) the average score,
(b) the number of students who failed, i.e. scored below 60 and,
(c) the number of students with perfect papers, i.e. scored 100.
***********************************************************************************
****************************************
integer a
dimension a(100)
open (1,file='p1.dat')
open (2,file='p2.dat')
read (1,*)n
do i=1,n
read (1,*)a(i)
end do
total=0.0
do i=1,n
total=total+a(i)
end do
avg=total/float(n)
k1=0
k2=0
do i=1,n
if(a(i)<60)k1=k1+1
if(a(i)==100)k2=k2+1
end do
write (2,*)'no. of student who has failed:',k1
write (2,*)'no. of student who has passed:',k2
write (2,*)'average number is',avg
close(1)
close(2)
stop
end

!!Input!!
...................................................................................
........................................
5
100
56
78
98
34
...................................................................................
........................................

#3.4
***********************************************************************************
****************************************
A student takes seven test (on which scored range from 0 to 100), and his final
grade is the
average of the six highest test scores. Assuming the seven test scores are punched
on a data card,
write a fortran program segment which finds the student's final grade.
***********************************************************************************
****************************************
dimension a(100)
open (1,file='p1.dat')
open (2,file='p2.dat')
read (1,*)n
do i=1,n
read (1,*)a(i)
end do
total=0.0
do i=1,n
total=total+a(i)
end do
small=a(1)
do i=1,n
if(a(i)<small)small=a(i)
end do
sum=total-small
ave=sum/float(n-1)
write (2,*)'small number is',small,'average number is',ave
close(1)
close(2)
stop
end

!!Input!!
...................................................................................
........................................
7
48
40
53
60
65
70
80
...................................................................................
........................................

#3.5
***********************************************************************************
****************************************
An NxN array A is in memory
(a)Write a program which sums the elements above the main diagonal i.e., those
elements A(I,J) such that I<J.
(b)Write a program which sums the elements above the main diagonal i.e., those
elements A(I,J) such that I>J.
***********************************************************************************
****************************************
dimension a(100,100)
open (1,file='p1.dat')
open (2,file='p2.dat')
read (1,*)n
x=0
y=0
z=0
do i=1,n
do j=1,n
read(1,*)a(i,j)
if(i<j)x=x+a(i,j)
if(i>j)y=y+a(i,j)
if(i==j)z=z+a(i,j)
end do
end do
write (2,*)'sum of elements above main diagonal:',x
write (2,*)'sum of elements below main diagonal:',y
write (2,*)'sum of elements in the main diagonal:',z
close(1)
close(2)
stop
end

!!Input!!
...................................................................................
........................................
3
2
3
4
5
6
7
8
9
10
...................................................................................
........................................

#3.6
***********************************************************************************
****************************************
Write a Fortran program to find the future value of $1000 in AB bank at an interest
rate
8% compounded (i)daily(ii)weekly and (iii)monthly after 5 years
***********************************************************************************
****************************************
open (1,file='p1.dat')
open (2,file='p2.dat')
read (1,*)p,n,r
read (1,*)a,b,c,d
e=p*(1+r/(100*a))**(n*a)
f=p*(1+r/(100*b))**(n*b)
g=p*(1+r/(100*c))**(n*c)
h=p*(1+r/(100*d))**(n*d)
write (2,*)'future value when interest daily:',e
write (2,*)'future value when interest weekly:',f
write (2,*)'future value when interest monthly:',g
write (2,*)'future value when interest yearly:',h
close(1)
close(2)
stop
end

!!Input!!
...................................................................................
........................................
1000
5
8
365
52
12
1
...................................................................................
........................................

#4.1(a)
***********************************************************************************
****************************************
Write a FORTRAN program for each of the given method to find the root of the
equation
cos x-x=0 correct to six decimal places.
(a)Bisection Method
***********************************************************************************
****************************************
real::a,b,tol,x,f
integer::i,n
open(1,file='a1.dat')
open(2,file='a2.dat')
read(1,*)a,b,n
tol=0.000001
write(2,5)
5 format(3x,'n',8x,'a',11x,'b'12x,'x'11x,'f')
do i=1,n
x=(a+b)/2
if(f(x)==0.or.abs(b-a)<=tol)then
go to 33
else if(f(x)*f(a)>0)then
a=x
else
b=x
end if
write(2,4)i,a,b,x,f(x)
4 format(2x,i3,2x,f10.6,2x,f10.6,2x,f10.6,2x,f10.6)
end do
write(2,*)'it is not sufficient'
stop
33 write(2,*)'solution is:',x
close(1)
close(2)
stop
end
function f(x)
implicit none
real::f
real,intent(in)::x
f=cos(x)-x
return
end

!!Input!!
...................................................................................
........................................
0
1
30
...................................................................................
........................................

#4.1(b)
***********************************************************************************
****************************************
Write a FORTRAN program for each of the given method to find the root of the
equation cos x-x=0
correct to six decimal places.
(b)Fixed point iteration method.
***********************************************************************************
****************************************
F(X)=COS(X)
DF(X)=-SIN(X)
OPEN(1,FILE='A1.DAT')
OPEN(2,FILE='A2.DAT')
READ(1,*)XO,N
TOL=0.000001
WRITE(2,5)
5 FORMAT(3X,'I',8X,'X')
DO I=1,N
IF(ABS(DF(X))>1)GO TO 11
X=F(XO)
WRITE(2,4)I,X
4 FORMAT(2X,I3,2X,F10.6)
IF(ABS(X-XO)<=TOL)GO TO 33
XO=X
END DO
11 WRITE(2,*)'THE ITERATION METHOD IS NOT POSSIBLE'
33 WRITE(2,*)'SOLUTION IS=',X
CLOSE(1)
CLOSE(2)
STOP
END

!!Input!!
...................................................................................
........................................
0.5
40
...................................................................................
........................................

#4.1(c)
***********************************************************************************
****************************************
Write a FORTRAN program for each of the given method to find the root of the
equation cosx-x=0 correct to six decimal places.
(c)Newton Rapphson method.
***********************************************************************************
****************************************
F(X)=COS(X)-X
DF(X)=-SIN(X)-1.0
OPEN(1,FILE='A1.DAT')
OPEN(2,FILE='A2.DAT')
READ(1,*)XO,N
TOL=0.000001
WRITE(2,5)
5 FORMAT(3X,'I',8X,'X')
DO I=1,N
X=XO-F(XO)/DF(XO)
WRITE(2,4)I,X
4 FORMAT(2X,I3,2X,F10.6)
IF(ABS(X-XO)<=TOL)GO TO 33
XO=X
END DO
33 WRITE(2,*)'SOLUTION IS=',X
CLOSE(1)
CLOSE(2)
STOP
END

!!Input!!
...................................................................................
........................................
0.5
40
...................................................................................
........................................

#4.2(a)
***********************************************************************************
****************************************
Write a FORTRAN program for Newton's Forward difference interpolation formula.
Hence, using
this program for the given
following set of tabulated values:
X 1961 1971 1981 1991 2001
Y=F(X ) 46 66 81 93 101
Find (i) F(1958) (ii) F(1965) (iii) F(1995) (iv) F(2010)
***********************************************************************************
****************************************
DIMENSION X(100),Y(100),D(100,100)
OPEN (1,FILE='A1.DAT')
OPEN (2,FILE='A2.DAT')
READ (1,*) N
READ (1,*)(X(I),Y(I),I=1,N)
READ (1,*)X0
DO I=1,N-1
D(I,1)=Y(I+1)-Y(I)
END DO
DO J=2,N-1
DO I=1,N-J
D(I,J)=D(I+1,J-1)-D(I,J-1)
END DO
END DO
WRITE (2,*)'FORWARD DIFFERENCE TABLE'
DO I=1,N
WRITE (2,12)X(I),Y(I),(D(I,J),J=1,N-I)
12 FORMAT (2X,F12.6,5(2X,F10.6))
END DO
H=X(2)-X(1)
P=(X0-X(1))/H
Y0=Y(1)
S=P
DO I=1,N-1
Y0=Y0+S*D(1,I)/FACT(I)
S=S*(P-I)
END DO
WRITE (2,*) X0,Y0
CLOSE (1)
CLOSE (2)
STOP
END
FUNCTION FACT(N)
FACT=1
DO I=1,N
FACT=FACT*I
END DO
END

!!Input!!
...................................................................................
........................................
5
1961 46
1971 66
1981 81
1991 93
2001 101
1958
...................................................................................
........................................

#4.2(b)
***********************************************************************************
****************************************
Write a FORTRAN program for Newton's Backward difference interpolation formula.
Hence, using
this program for the given
following set of tabulated values:
X 1961 1971 1981 1991 2001
Y=F(X ) 46 66 81 93 101
Find (i) F(1958) (ii) F(1965) (iii) F(1995) (iv) F(2010)
***********************************************************************************
****************************************
DIMENSION X(100),Y(100),D(100,100)
OPEN (1,FILE='A1.DAT')
OPEN (2,FILE='A2.DAT')
READ (1,*) N
READ (1,*)(X(I),Y(I),I=1,N)
READ (1,*)X1
DO I=2,N
D(I,1)=Y(I)-Y(I-1)
END DO
DO J=2,N-1
DO I=J+1,N
D(I,J)=D(I,J-1)-D(I-1,J-1)
END DO
END DO
WRITE (2,*)'BACKWARD DIFFERENCE TABLE'
WRITE (2,13)X(1),Y(1)
13 FORMAT (2X,F12.6,2X,F10.6)
DO I=2,N
WRITE (2,14)X(I),Y(I),(D(I,J),J=1,I-1)
14 FORMAT (2X,F12.6,5(2X,F10.6))
END DO
H=X(2)-X(1)
Q=(X1-X(N))/H
Y1=Y(N)
T=Q
DO I=1,N-1
Y1=Y1+T*D(N,I)/FACT(I)
T=T*(Q+I)
END DO
WRITE (2,*) X1,Y1
CLOSE (1)
CLOSE (2)
STOP
END
FUNCTION FACT(N)
FACT=1
DO I=1,N
FACT=FACT*I
END DO
END

!!Input!!
...................................................................................
........................................
5
1961 46
1971 66
1981 81
1991 93
2001 101
2010
...................................................................................
........................................
#4.3
***********************************************************************************
****************************************
Write a FORTRAN program for Lagrange's interpolation formula and hence compute
the value of log10 301 for the following table:
X 300 304 305 307
y=f(x)= 2.4771 2.4829 2.4843 2.4871
log10 x
***********************************************************************************
****************************************
DIMENSION X(20),Y(20)
OPEN(1,FILE='A1.DAT')
OPEN(2,FILE='A2.DAT')
READ(1,*)N
READ(1,*)(X(I),Y(I),I=1,N)
READ(1,*)XO
YO=0
DO I=1,N
XU=1
DE=1
DO J=1,N
IF(I.NE.J)THEN
XU=XU*(XO-X(J))
DE=DE*(X(I)-X(J))
END IF
END DO
YO=YO+(XU/DE)*Y(I)
END DO
WRITE(2,*)'Y=',YO
CLOSE(1)
CLOSE(2)
STOP
END PROGRAM

!!Input!!
...................................................................................
........................................
4
300 2.4771
304 2.4829
305 2.4843
307 2.4871
301
...................................................................................
........................................

#5.1
***********************************************************************************
****************************************
A department store chain has 6 stores and each store has the same 12
department. The weekly sales of the chain are stored in an 6x12x7 array SALES
such that sales (I,J, K) denotes the sales in the I th store, in the J th
department, on
the K th day. Write a FORTRAN program segment which
(a) prints the total weekly sales of each stores;
(b)prints the total weekly sales of each department;
(c)prints the total weekly sales of each chain.
***********************************************************************************
****************************************
DIMENSION SA(100,100,100)
OPEN (1,FILE='y1.DAT')
OPEN (2,FILE='y2.DAT')
READ (1,*) L,M,N
READ (1,*)(((SA(I,J,K),K=1,N),J=1,M),I=1,L)
TCS=0
DO I=1,L
TSS=0
DO J=1,M
DAYS=0
DO K=1,N
DAYS=DAYS+SA(I,J,K)
END DO
TSS=TSS+DAYS
TCS=TCS+DAYS
END DO
WRITE (2,*)I,TSS
END DO
WRITE (2,*)'/'
DO J=1,M
TSD=0
DO I=1,M
DAYS=0
DO K=1,N
DAYS=DAYS+SA(I,J,K)
END DO
TSD=TSD+DAYS
END DO
WRITE (2,*)J,TSD
END DO
WRITE (2,*)TCS
CLOSE (1)
CLOSE (2)
STOP
END

!!Input!!
...................................................................................
........................................
2 2 2
1000
2000
3000
4000
5000
6000
7000
8000
...................................................................................
........................................

#5.2
***********************************************************************************
****************************************
Write a FORTRAN program for numerical evaluation of the integral 𝒇(𝒙)𝒅𝒙 with limit a
to b
using (i) Trapezoidal Rule (ii) Simpson's 1/3 Rule (iii) Simpson's - 3/8 Rule and
(iv) Weddle's Rule. Hence, evaluate integral (1/x^2)𝒅𝒙 with limit 0 to 1. correct
up to 5 decimal places and
compare the result with the exact value and comment on the results.
***********************************************************************************
****************************************
OPEN (1,FILE='t1.DAT')
OPEN (2,FILE='t2.DAT')
READ (1,*) A,B,N
H=(B-A)/FLOAT(N)
SUM1=F(A)+F(B)
SUM2=0
DO I=1,N-1
X=A+I*H
SUM2=SUM2+F(X)
END DO
TAPI=(H/2.0)*(SUM1+2*SUM2)
WRITE (2,*)'TAPI=',TAPI
SUM2=0
SUM3=0
DO I=1,N-1
X=A+I*H
IF(MOD(I,2)==0)THEN
SUM2=SUM2+F(X)
ELSE
SUM3=SUM3+F(X)
END IF
END DO
SIM_1_3=(H/3.0)*(SUM1+2*SUM2+4*SUM3)
WRITE (2,*)'SIMP_1_3=',SIM_1_3
SUM2=0
SUM3=0
DO I=1,N-1
X=A+I*H
IF(MOD(I,3)==0)THEN
SUM2=SUM2+F(X)
ELSE
SUM3=SUM3+F(X)
END IF
END DO
SIM_3_8=(3*H/8.0)*(SUM1+2*SUM2+3*SUM3)
WRITE (2,*)'SIMP_3_8=',SIM_3_8
SUM2=0
SUM3=0
SUM4=0
SUM5=0
DO I=1,N-1
X=A+I*H
IF(MOD(I,6)==0)THEN
SUM2=SUM2+F(X)
ELSEIF(MOD(I,3)==0)THEN
SUM4=SUM4+F(X)
ELSEIF(MOD(I,2)==0)THEN
SUM5=SUM5+F(X)
ELSE
SUM3=SUM3+F(X)
END IF
END DO
WIDDLE=(3*H/10.0)*(SUM1+2*SUM2+6*SUM4+SUM5+5*SUM3)
WRITE (2,*)'WIDDLE=',WIDDLE
CLOSE (1)
CLOSE (2)
STOP
END
FUNCTION F(X)
F=1/(1+x**2)
END

!!Input!!
...................................................................................
........................................
1 0 12
...................................................................................
........................................

#5.3
***********************************************************************************
****************************************
Write a FORTRAN program for Romberg integration to compute integration of 1/(1+x^2)
with limit 0 to 1.
correct up to 3 decimal places.
***********************************************************************************
****************************************
DIMENSION R(10,10)
OPEN (1,FILE='A1.DAT')
OPEN (2,FILE='A2.DAT')
READ (1,*)A,B,N
R(1,1)=0.5*H(A,B,1)*(F(A)+F(B))
DO I=2,N
SUM=0
DO J=1,2**(I-2)
SUM=SUM+F(A+(2*J-1)*H(A,B,I))
END DO
R(I,1)=0.5*(R(I-1,1)+H(A,B,I-1)*SUM)
END DO
DO I=2,N
DO J=2,I
R(I,J)=R(I,J-1)+(R(I,J-1)-R(I-1,J-1))/(4**(J-1)-1)
END DO
END DO
DO I=1,N
WRITE (2,11)(R(I,J),J=1,I)
11 FORMAT (1X,21(3X,F10.7))
END DO
CLOSE (1)
CLOSE (2)
STOP
END
FUNCTION H(A,B,K)
H=(B-A)/M(K)
END
FUNCTION M(K)
M=2**(K-1)
END
FUNCTION F(X)
F=1.0/(1+X)
END

!!Input!!
...................................................................................
........................................
1 0 2
...................................................................................
........................................

#5.4
***********************************************************************************
****************************************
Write a FORTRAN program to solve the following system of linear equations by
using (i) Gaussian elimination method
X1 +X2 – X3=2
2X1+3X2+5X3=-3
3X1+2X2+3X3=6
***********************************************************************************
****************************************
PARAMETER (N=3)
DIMENSION A(10,10),X(10)
OPEN (1,FILE='A1.DAT')
OPEN (2,FILE='A2.DAT')
READ (1,*)((A(I,J),J=1,N+1),I=1,N)
DO I=1,N-1
DO J=I+1,N
MLT=A(J,I)/A(I,I)
DO K=1,N+1
A(J,K)=A(J,K)-A(I,K)*MLT
END DO
END DO
END DO
X(N)=A(N,N+1)/A(N,N)
DO I=N-1,1,-1
SUM=0
DO J=I+1,N
SUM=SUM+A(I,J)*X(J)
END DO
X(I)=(A(I,N+1)-SUM)/A(I,I)
END DO
WRITE (2,*)(X(I),I=1,N)
CLOSE (1)
CLOSE (2)
STOP
END

!!Input!!
...................................................................................
........................................
1 1 -1 2
2 3 5 -3
3 2 3 6
...................................................................................
........................................

You might also like