Professional Documents
Culture Documents
Physics Department
Computer
Program One:
Solution:
program One Implicit none real::a,v1,v2,x2,x1 read*,v2,v1,x2,x1 a= ((v2**2)-(v1**2))/(x2-x1) print*, 'The Result is',a end program One
x2= 3.00000
x1= 5.00000
2012-2013
Physics Department
Computer
Program Two:
Write a fortran95 program to convert the angle from degree to radian. Solution:
program Two Implicit none real::s,d,r read*,d s=3.14/180 r=s*d print*,'The Result is',r end program Two
d=
90.0000 1.57000
The Result is
2012-2013
Physics Department
Computer
Program Three:
Write a fortran95 program to convert the angle from degree to radian. Solution:
program Three Implicit none real::s,d,r,pi read*,d pi=4.*atan(1.0) s=pi/180 r=s*d print*,'The Result is',r end program Three
d=
90.0000 1.57080
The Result is
2012-2013
Physics Department
Computer
Program Four:
Write a fortran95 program to convert the angle from radian to degree. Solution:
program Four Implicit none real::s,d,r read*,r s=180/3.14 d=s*r print*,'The Result is',d end program Four
r=
3.14000 180.000
The Result is
2012-2013
Physics Department
Computer
Program Five:
Solution:
program Five Implicit none real::y,a,b,c,x read*,a,b,c,x y=a*x**2+b*x+c print*,a,b,c, 'The Result is',y end program Five
a=
9.00000
b=
6.00000
c=
4.00000
x=
7.00000
The Result is
487.000
2012-2013
Physics Department
Computer
Program Six:
Solution:
program Six Implicit none real::z,x,pi,d,r read*,d pi=(4.0)*atan(1.0) r=(d*pi)/180 z=sin(r)+cos(r) print*,r, 'The Result is',z end program Six
d=
45.0000 1.41421
The Result is
2012-2013
Physics Department
Computer
Program Seven:
Solution:
program Seven Implicit none real::z,pi,d,r read*,d pi=(4.0)*atan(1.0) r=(d*pi)/180 z=exp(sin(r)+cos(r)) print*,r, 'The Result is',z end program Seven
d=
180.000 0.367879
The Result is
2012-2013
Physics Department
Computer
Program Eight:
Solution:
program Eight Implicit none real::z,pi,d,r read*,d pi=(4.0)*atan(1.0) r=(d*pi)/180 z=exp(abs(sin(r)+cos(r))) print*,r, 'The Result is',z end program Eight
d=
180.000 2.71828
The Result is
2012-2013
Physics Department
Computer
Program Nine:
Solution:
program Nine Implicit none real::z,x read*,x z=sin(acos(x+0.5)) print*,'The Result is',z end program Nine
x=
0.00000 0.866025
The Result is
2012-2013
Physics Department
Computer
Program Ten:
Use elseif to write a fortran95 program that read a number. Get the program to decide: If the number is equal to 1, then If the number is equal to 2, then
2012-2013
10
Physics Department
Computer
2012-2013
11
Physics Department
Computer
Program Eleven:
Use elseif to write a fortran95 program that read a number. Get the program to decide: If the number greater than 10, then If the number between -10 to 10, then If the number is equal to 10, then Solution:
program Eleven Implicit none real::x,y,pi read*,x pi=(4.0)*atan(1.0) if(x>10)then y=x**2+cos(pi*x/180) elseif(x>=-10.and.x<10)then y=exp(2*x)+sqrt(5*x)+10 elseif(x==10)then y=x-1 endif print*,'The Result is',y end program Eleven
2012-2013
12
Physics Department
Computer
x=
13.0000 169.974
The Result is
x=
4.00000 2995.43
The Result is
x=
10.0000 9.00000
The Result is
2012-2013
13
Physics Department
Computer
Program Twelve:
Write a fortran95 program that read a number. Get the program to decide: If the number equal to 0, then stop the program If the number greater than 1, then print greater than 1 If the number less than 1, then print less than 1 Solution:
program Twelve Implicit none real::x,y,pi read*,x if(x==0)stop if(x>1)print*,'greater than 1' if(x<1)print*,'less than 1' end program Twelve
x= x=
47.0000 -21.0000
2012-2013
14
Physics Department
Computer
Program Thirteen:
Use write and format to write a fortran95 program which calculate . Solution:
Program Thirteen Implicit none integer::k do k=1,20 write(*,10)k,k**2,k**3 enddo 10 format(i4,i6,i12) end program Thirteen
1 2 3 4 5 6 7 8 9 10
1 4 9 16 25 36 49 64 81 100
11 12 13 14 15 16 17 18 19 20
121 144 169 196 225 256 289 324 361 400
1331 1728 2197 2744 3375 4096 4913 5832 6859 8000
2012-2013
15
Physics Department
Computer
Program Fourteen:
Use do while to write a fortran95 program to calculate the following summation when (s) is less than 120: Solution: First
program Fourteen implicit none real::s,x read*,x s=0 while(s.le.120)do s=s+x**2 write(*,*)'The Result is',s enddo end program Fourteen
x=
The Result is
2012-2013
16
Physics Department
Computer
Second
program Fourteen implicit none real::s,x read*,x s=0 do while(s.le.120) s=s+x**2 write(*,*)'The Result is',s enddo end program Fourteen
x=
6.00000
The Result is
2012-2013
17
Physics Department
Computer
Program Fifteen:
Write a fortran95 program that read a number. Get the program to decide: If the number greater than 1, then print greater than 1 If the number less than 1, then print less than 1 If the number equal to 10, then exit the program Solution:
program Fifteen Implicit none real::x,y,pi integer::i do i=1,4 read*,x if(x>1)print*,'greater than 1' if(x<1)print*,'less than 1' if(x==10)exit enddo end program Fifteen
x= x= x=
Note: EXIT has the same role as STOP, but EXIT only use in Do Loop program. EXIT will leave a loop.
2012-2013
18
Physics Department
Computer
Program Sixteen:
Write a fortran95 program that read a number. Get the program to decide: If the number equal to 0, then stop the program If the number greater than 1, then print greater than 1 If the number less than 1, then print less than 1 Solution:
program Sixteen Implicit none real::x,y,pi do read*,x if(x==0)stop if(x>1)print*,'greater than 1' if(x<1)print*,'less than 1' enddo end program Sixteen
x= x= x= x=
2012-2013
19
Physics Department
Computer
Program Seventeen:
Use goto to write a fortran95 program that evaluates the following series: Solution:
program Seventeen Implicit none real::x,s,i read*,x i=0 s=0.0 10 i=i+1 s=s+x**i if(i<10)goto 10 print*,'The Result is',s end program Seventeen
x=
4.00000 1.398100E+06
The Result is
2012-2013
20
Physics Department
Computer
Program Eighteen:
Use goto to write a fortran95 program that reads a reak number (n). The program then computes and prints the factorial of (n): Solution:
program Eighteen Implicit none real::n,f,j read*,n f=1.0 j=0 10 j=j+1 f=f*j if(j<n)goto 10 print*,n, 'The Result is',f end program Eighteen
n=
7.00000 5040.00
The Result is
2012-2013
21
Physics Department
Computer
Program Nineteen:
n=
The Result is
2012-2013
22
Physics Department
Computer
Program Twenty:
i=
1 2 3 4 5
The Result is
2012-2013
23
Physics Department
Computer
n= x=
The Result is
2012-2013
24
Physics Department
Computer
Write a fortran95 program to compute and print the factorial of the real number: Solution:
program TwentyTwo Implicit none integer::i real::p,n read*,n p=1 do i=1,n p=p*i print*,'The Result is',p enddo end program TwentyTwo
n=
4.00000
The Result is
2012-2013
25
Physics Department
Computer
n= x=
The Result is
2012-2013
26
Physics Department
Computer
n=
21.0000
x=
8.093233E+06
2012-2013
27
Physics Department
Computer
n=
21.0000
x=
4.00000
The Result is
54.5981
2012-2013
28
Physics Department
Computer
Write a fortran95 program to calculate the exponential of (x) and the following series separately: Solution:
program TwentySix Implicit none integer::i,j real::x,s,n,p read*,n,x s=1 do i=1,n p=1 do j=1,i p=p*j enddo s=s+(x**i)/p print*,'n=',n,'x=',x,'The Result is',s,'exp(',x,')',exp(x) enddo end program TwentySix
n=
20.0000
x=
The Result is
20.0855 , exp(
2012-2013
29
Physics Department
Computer
Write a fortran95 program to calculate the exponential (-x) and the following series separately: Solution:
program TwentySeven Implicit none integer::i,j real::x,s,n,p read*,n,x s=1 do i=1,n p=1 do j=1,i p=p*j enddo s=s+((-x)**i)/p print*,'n=',n,'x=',x,'The Result is',s,'exp(',-x,')',exp(-x) enddo end program TwentySeven
n=
20.0000
x=
2012-2013
30
Physics Department
Computer
n= x=
2.00000 2.00000
m= y=
3.00000 3.00000
The Result is
234.000
2012-2013
31
Physics Department
Computer
2012-2013
32
Physics Department
Computer
n= m= x= y=
The Result is
2012-2013
33
Physics Department
Computer
Program Thirty:
Solution:
program Thirty Implicit none integer::i,k real::x,s,n,p read*,n,x s=0 do i=1,n p=1 do k=1,i p=p*k enddo s=s+((-1)**(i+1))*(x**i)/(p) print*,'The Result is',s enddo end program Thirty
n=
9.00000
x=
2.00000
The Result is
0.864903
2012-2013
34
Physics Department
Computer
Solution:
program ThirtyOne Implicit none integer::i,k,m real::x,s,p read*,x s=1 ; m=1 do i=3,7,2 p=1 do k=1,i-1 ; p=p*k ; enddo m=m+1 s=s+(-1)**m*((-x)**i)/(p) print*,'m=',m,'The Result is',s enddo end program ThirtyOne
x= x= x=
m= m= m=
2012-2013
35
Physics Department
Computer
Program Thirty Two: Create a file and write 10 numbers in it. Write a fortran95 program to read that file and find the average of summation of the 10 numbers:
Solution:
Program ThirtyTwo Implicit none integer::i Real::s,av,num Open(12,file='mydata.txt') S=0 Do i=1, 10 Read(12,*)num s=s+num enddo av=s/10 print*,s, 'The Result is',av end program ThirtyTwo
s=
55.0000 5.50000
The Result is
2012-2013
36
Physics Department
Computer
Program Thirty Three: Create a file and write 10 numbers in it. Write a fortran95 program to read that file and determine even and odd numbers and separate them into two different files:
Solution:
program ThirtyThree implicit none integer::i,num,x,y open(10,file="mydata.txt") open(11,file="odd.txt") open(12,file="even.txt") do i=1,10 read(10,*)num if(mod(num,2)>0)then x=num write(11,*)x,"odd" else y=num write(12,*)y,"even" print*,'odd=',x,'even',y end if end do end program ThirtyThree
2012-2013
37
Physics Department
Computer
1 3 5 7 9
2 4 6 8 10
2012-2013
38
Physics Department
Computer
Program Thirty Four: Create a file and write 15 numbers in it. Write a fortran95 program to read that file and determine positive and negative numbers and separate them into two different files:
Solution:
program ThirtyFour implicit none integer::i,num,x,y open(10,file="mydata.txt") open(11,file="positive.txt") open(12,file="negative.txt") do i=1,15 read(10,*)num if(num>0)then x=num write(11,*)x,"positive" else y=num write(12,*)y,"negative" end if end do end program ThirtyFour
**Output in files
2012-2013 39
Physics Department
Computer
Program Thirty Five: Write a fortran95 program to calculate the following equation by using the precision to 15 figures.
Solution:
program ThirtyFive implicit none integer,parameter::ikind=selected_real_kind(p=15) real(kind=ikind)::s integer:: i s=0 do i=1,6 s=s+1./i**7 print*,'The Result is',s enddo end program ThirtyFive
The Result is
2012-2013
40
Physics Department
Computer
Program Thirty Six: Write a fortran95 program to calculate the following equation by using the precision to 18 figures.
Solution:
program ThirtySix implicit none integer,parameter::ikind=selected_real_kind(p=18) real(kind=ikind)::s integer:: i s=0 do i=1,6 s=s+1./i**7 print*,'The Result is',s enddo end program ThirtySix
The Result is
2012-2013
41
Physics Department
Computer
Program Thirty Seven: Write a fortran95 program is to compare the real and integer for precision to 18 figures.
Solution:
Program ThirtySeven Implicit none integer,parameter::ikind=selected_real_kind(p=18) real(kind=ikind):: v1,v2,v3,v4,v5,v6,x,y v1=10/3 print*,'The Value of v1 is',v1 x=10 y=3 v2=x/y print*,'The Value of v2 is',v2 v3=10.0_ikind/3 print*,'The Value of v3 is',v3 v4=10.0/3.0 print*,'The Value of v4 is',v4 v5=0.1234567890123456789_ikind print*,'The Value of v5 is',v5 v6=0.1234567890123456789 print*,'The Value of v6 is',v6 end ThirtySeven
2012-2013
42
Physics Department
Computer
The Value of v1 is The Value of v2 is The Value of v3 is The Value of v4 is The Value of v5 is The Value of v6 is
2012-2013
43
Physics Department
Computer
Program Thirty Eight: Write a fortran95 program to calculate sin(x) by using the precision to 18 figures.
Solution:
Program ThirtyEight Implicit none integer,parameter::ikind=selected_real_kind(p=18) real(kind=ikind)::s,x,p,pi integer::i,k read*,x pi=4.*atan(1.0) x=x*pi/180 s=x do i=1,20 p=1 do k=1,2*i+1; p=p*k ; enddo s=s+(-1)**i*x**(2*i+1)/p enddo print*,'The Result is',s end ThirtyEight
x=
0.78539818525314331 0.70710679664085750
The Result is
2012-2013
44
Physics Department
Computer
Program Thirty Nine: Write a fortran95 program to calculate the following equation by using the precision to 18 figures. Read the (x) in degree
Solution:
Program ThirtyNine Implicit none integer,parameter::ikind=selected_real_kind(p=18) real(kind=ikind)::s,x,g,p,z,f,pi integer::i,k,l,j read*,x s=x do i=1,20 p=1 do k=1,2*i+1 p=p*k enddo s=s+(-1)**i*x**(2*i+1)/p enddo g=1 do j=1,20 f=1 do l=1,2*j f=f*l
2012-2013
45
Physics Department
Computer
x= sin( cos(
The Result is
1.0007960096425680
2012-2013
46
Physics Department
Computer
Program Forty: Write a fortran95 program to calculate the following equation by using the precision to 15 figures. Read the (x) in radian.
Solution:
program Forty implicit none integer,parameter::ikind=selected_real_kind(p=15) real(kind=ikind)::s1,s2,p,s3,f,x,pi integer:: i,j,h,b read*,x pi=4.0*atan(1.0) x=x*pi/180 s1=x do i=1,20 p=1 do j=1,2*i+1 p=p*j end do s1=s1+(-1)**i*x**(2*i+1)/p end do s2=1 do h= 1,20 f=1
2012-2013
47
Physics Department
Computer
do b=1,2*h f=f*b end do s2=s2+(-1)**h*x**(2*h)/f end do s3=s1**2+s2**2 print*,s1,s2,'The Result is',s3 end program Forty
x= sin( cos(
The Result is
1.00000000000
2012-2013
48
Physics Department
Computer
Program Forty One: Write a fortran95 program to calculate the sinh(x) and cosh(x) to find the value of tanh(x) and coth(x) and by using the precision to 18 figures.
Solution:
program FortyOne implicit none integer,parameter::ikind=selected_real_kind(p=18) real(kind=ikind)::s1,s2,s4,x,p,s3,f,s5 integer:: i,j,h,b read*,x s1=x do i=1,20 p=1 do j=1,2*i+1 p=p*j end do s1=s1+x**(2*i+1)/p end do s2=1 do h= 1,20 f=1 do b=1,2*h f=f*b
2012-2013
49
Physics Department
Computer
end do s2=s2+x**(2*h)/f end do s3=s1/s2 s4=s2/s1 s5=s1+s2 print*,'tanh(',x,')=',s3 print*,'coth(',x,')=',s4 print*,'[cosh(',x,')+sinh(',x,')=]',s5 print*,'exp(',x,')',exp(x) end program FortyOne
tanh( coth(
1.0000000000000000 1.0000000000000000
)= 0.76159415595576489 )= 1.3130352854993313
tanh( coth(
2.0000000000000000 2.0000000000000000
)= )=
0.96402758007581688 1.0373147207275481
2012-2013
50