You are on page 1of 51

Computer Laboratory Fortran 95 Part1

Prepared by: Saro S. Ahmed, Kani M. Rauf, Hossien Hossieni 2013

Physics Department

Computer

Program One:

Write a fortran95 program to calculate the following equation:

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

v2=9.00000 The Result is

v1= 6.00000 -22.5000

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:

Write a fortran95 program to calculate (y) from the following equation:

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:

Write a fortran95 program to find the value of (z):

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:

Write a fortran95 program to calculate the following exponential equation:

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:

Write a fortran95 program to calculate the following equation:


| |

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:

Write a fortran95 program to calculate the following equation:

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

Otherwise print the suitable message to inform the user. Solution:


program Ten Implicit none real::x,y,x1,x2,t integer::num read*,num if(num==1)then read*,x y=(x)**(2.5)+sqrt(x) else if(num==2)then read*,x2,x1,t y=(x2-x1)/(t**2) else print*,'enter the No. 1 or 2' endif print*,'The Result is',y end program Ten

2012-2013

10

Physics Department

Computer

num= 1 x= 3.00000 The Result is 17.3205

num=2 x2= 5.00000 x1= 2.00000 t=6.00000 The Result is 8.333334E-02

num= 5 Enter the No. 1 or 2

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

greater than 1 less than 1

x= 0 Stop the program

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

1 8 27 64 125 216 343 512 729 1000

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=

6.00000 36.0000 72.0000 108.000 144.000

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

36.0000 72.0000 108.000 144.000

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=

5.00000 -9.00000 10.0000

greater than 1 less than 1 greater than 1

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=

8.00000 -5.00000 2.00000 0.00000

greater than 1 less than 1 greater than 1 Stop the program

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:

Write a fortran95 program to calculate the following series: Solution:


program Nineteen Implicit none integer::i real::s,n s=0.0 read*,n do i=1,n s=s+i print*,'The Result is',s enddo end program Nineteen

n=

4.00000 1.00000 3.00000 6.00000 10.0000

The Result is

2012-2013

22

Physics Department

Computer

Program Twenty:

Write a fortran95 program to calculate the following series: Solution:


program Twenty Implicit none integer::i real::x x=0.0 do i=1,5 x=x+(2*i)**3 print*, i, 'The Result is', x enddo end program Twenty

i=

1 2 3 4 5

The Result is

8.00000 72.0000 288.000 800.000 1800.00

2012-2013

23

Physics Department

Computer

Program Twenty One:

Write a fortran95 program to calculate the following series: Solution:


program TwentyOne Implicit none integer::i real::x,s,n s=0.0 read*,n,x do i=1,n s=s+(x**i) print*,x, 'The Result is',s enddo end program TwentyOne

n= x=

5.00000 2.00000 62.0000

The Result is

2012-2013

24

Physics Department

Computer

Program Twenty Two:

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

1.00000 2.00000 6.00000 24.0000

2012-2013

25

Physics Department

Computer

Program Twenty Three:

Write a fortran95 program to evaluate the following series: Solution:


program TwentyThree Implicit none integer::i real::x,s,n read*,n,x s=0 do i=1,n s=s+x**(2*i) print*,'The Result is',s enddo end program TwentyThree

n= x=

21.0000 4.00000 2.063233E+25

The Result is

2012-2013

26

Physics Department

Computer

Program Twenty Four:

Write a fortran95 program to evaluate the following series: Solution:


program TwentyFour Implicit none integer::i,j real::x,s,n,p read*,n,x s=0 do i=1,n p=1 do j=1,i p=p*j enddo s=s+x**(2*i)/p print*,'The Result is',s enddo end program TwentyFour

n=

21.0000

x=

4.00000 The Result is

8.093233E+06

2012-2013

27

Physics Department

Computer

Program Twenty Five:

Write a fortran95 program to evaluate the following series: Solution:


program TwentyFive 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*,'The Result is',s enddo end program TwentyFive

n=

21.0000

x=

4.00000

The Result is

54.5981

2012-2013

28

Physics Department

Computer

Program Twenty Six:

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=

3.00000 3.00000 ) 20.0855

The Result is

20.0855 , exp(

2012-2013

29

Physics Department

Computer

Program Twenty Seven:

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=

3.00000 -3.00000 ) 4.978707E-02

The Result is 4.978708E-02 , exp(

2012-2013

30

Physics Department

Computer

Program Twenty Eight:

Write a fortran95 program to calculate the following equation: Solution:


program TwentyEight Implicit none integer::i,j real::x,s,n,m,y read*,n,m,x,y s=0 do i=1,n do j=1,m s=s+(x**i)*(y**j) print*,'The Result is',s enddo enddo end program TwentyEight

n= x=

2.00000 2.00000

m= y=

3.00000 3.00000

The Result is

234.000

2012-2013

31

Physics Department

Computer

Program Twenty Nine:

Write a fortran95 program to calculate the following equation: Solution:


program TwentyNine Implicit none integer::i,j,l,k real::x,s,n,p,m,y,f read*,n,m,x,y s=0 do i=1,n p=1 do k=1,i p=p*k enddo do j=1,m f=1 do l=1,j f=f*l enddo s=s+(x**i)*(y**j)/(f*p) print*,'The Result is',s

2012-2013

32

Physics Department

Computer

enddo enddo end program TwentyNine

n= m= x= y=

8.00000 9.00000 1.00000 1.00000 2.95249

The Result is

2012-2013

33

Physics Department

Computer

Program Thirty:

Write a fortran95 program to calculate the following series:

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

Program Thirty One:

Write a fortran95 program to calculate the following series:

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=

3.00000 3.00000 3.00000

m= m= m=

2The Result is 3The Result is 4The Result is

-12.5000 -2.37500 -5.41250

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

odd odd odd odd odd

1 3 5 7 9

even even even even even

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

1.00000000000 1.00781250000 1.00826974737 1.00833078253 1.00834358253 1.00834715477

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

1.0000000000000000 1.0078125000000000 1.0082697473708276 1.0083307825270776 1.0083435825270776 1.0083471547721622

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

3.0000000000000000 3.3333333333333333 3.3333333333333333 3.3333332538604736 0.12345678901234568 0.12345679104328156

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

enddo g=g+(-1)**j*x**(2*j)/f enddo z=s+g print*,s,g,'The Result is',z end ThirtyNine

x= sin( cos(

1.5700000000000000 1.5700000000000000 1.5700000000000000 )= )= 0.99999968293183462 7.96326710733325455E-04

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(

360.000000000 6.28318548203 6.28318548203 )= )= 1.748455620670E-07 1.00000000000

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

[cosh(1.0000000)+sinh(1.000000)]= 2.7182818284590452 exp( 1.0000000000000000 )= 2.7182818284590452

tanh( coth(

2.0000000000000000 2.0000000000000000

)= )=

0.96402758007581688 1.0373147207275481

[cosh( 2.00000000)+sinh( 2.000000)]= 7.3890560989306502 exp( 2.0000000000000000 )= 7.3890560989306502

2012-2013

50

You might also like