de cuong fortran

Màu nền
Font chữ
Font size
Chiều cao dòng

1.VD 16: Tính tích phân theo công thức simpson khi đoạn [a,b] chia 2n phần bằng nhau

integer n,i,k

            real h,x,s,a,b

            intrinsic mod

            external f

            print *,'Nhap hai can tich phan'

            read *,a,b

            print *,'Nhap so diem chia'

            read *,n

            h=(b-a)/(2*n)

            s=f(a)+f(b)

            do 10 i=1,2*n-1

            if(mod(i,2).eq.0)then

            s=s+2*f(x)

            else

            s=s+4*f(x)

            end if

10        continue

            s=s*h/3

            print 20,s

20        format ('Tich phan xap xi=',f9.2)

            end

            real function f(x)

            real x

            f=1/(1+x*x)

            end

2. VD 29: dãy fibonacci

            integer i,f,f1,f2,n

            f1=1

            f2=1

            f=0

            print *,'Tim so fibonacci thu:'

            read *,n

            do 10 i=3,n

            f=f1+f2

            f1=f2

            f2=f

10        continue

            print *,f

            end

3.VD 15: Tính khai triển Taylor

            real x,n,k,emu

            print *,'Nhap so hang n'

            read *,n

            print *,'Nhap gia tri x'

            read *,x

            emu=1.0

            s=1.0

            do 10 k=1,n

            s=s*x/k

            emu=emu+s

10        continue

            write (*,20)emu

20        format ('emu x gan bang',f20.5)

            End

4.VD 17: TÌnh nghiệm gần đúng f(x)=0 bằng phương pháp Newton

            real x1,x0,err,efxilon,x

            intrinsic abs

            external f,df

            print *,'Nhap gia tri diem dau:'

            read *,x0

            print *,'Nhap gia tri chinh xac:'

            read *,efxilon

10        x1=x0-f(x0)/df(x0)

            err=abs(x1-x0)

            if(err.lt.efxilon)then

            print 20,x1

            else

            x0=x1

            goto 10

            end if

20        format('Nghiem xap xi=',f20.5)

            end

            real function f(x)

            real x

            f=x*2-4

            end

            real function df(x)

            real x

            df=2*x

end

5.VD11: Tình nghiệm gần đúng của phương trình f(x)=0 bằng phương pháp chia đôi (f(x) liên tục và F(a)*f(b)<0)

            real a,b,c,x,y

            intrinsic abs

            external f

            print *,'Nhap hai dau a,b:'

            read *,a,b

            print *,'Nhap do do chinh xac efxilon:'

            read *,c

            if(f(a).eq.0)then

            print *,'Nghiem=',b

            stop

            endif

10        y=(b+a)/2

            if(f(y).lt.0)a=y

            if(f(y).gt.0)b=y

            if(abs(a-b).lt.c) then

            print *,'Nghiem xap xi=',y

            else

            goto 10

            end if

            end

            real function f(x)

            real x

6. VD8: Giải phương trình bậc hai ax2 + bx + c=0

real a,b,c,x1,x2,delta

            intrinsic  sqrt

            print *,'Nhap cac he so a,b,c cua tam thuc'

            read *,a,b,c

            delta=b*b-4*a*c

            if(delta.gt.0) then

            x1=(-b+sqrt(delta))/(2*a)

            x2=(-b-sqrt(delta))/(2*a)

            write (*,10) x1,x2

            else

            if(delta.eq.0) then

            write (*,20) -b/(2*a)

            else

            print *,'Phuong trinh khong co nghiem thuc'

            endif

            endif

10        format ('Nghiem x1=',f10.2,'Nghiem x2=',f10.2)

20        format ('Nghiem kep=',f10.2)

            End

7.

8.VD26: Kiểm tra một số nguyên dương là một số nguyên tố, n nhập vào

real *8 n

            intrinsic sqrt,int,mod

            logical kiem_tra_nguyen_to

            print *,'Nhap mot so nguyen duong:'

            read *,n

            if(kiem_tra_nguyen_to(n))then

            print 30,n

            else

            print 40,n

            endif

30        format (f30.1,'la nguyen to')

40        format (f30.1,'Khong phai la nguyen to')

            end

            logical function kiem_tra_nguyen_to(m)

            integer *4 l

            real *8 m,k

            logical kt

            kt=.true.

            l=int (sqrt(m))

            do 10 k=2,l

            if(mod(m,k).eq.0.0)then      kt=.false.

            goto 20

10        continue

20        kiem_tra_nguyen_to=kt

            end

8.VD 27: Đếm các số nguyên tố <= n cho trước, n nhập vào

real *8 n,k

            integer dem

            intrinsic sqrt,int,mod

            logical dem_so_nt

            external dem_so_nt

            print *,'Nhap mot so nguyen duong:'

            read *,n

            dem=0

            do 10 k=1,n

            if(dem_so_nt(k))then dem=dem+1

10        continue

            print 30,dem,n

30        format ('so cac so nguyen to nho hon=',f10.1,'la:',i10)

            end

            logical function dem_so_nt(m)

            integer *4 l

            real *8 m,k

            logical kt

            kt=.true.

            l=int(sqrt(m)) 

            do 40 k=2,l

            if(mod(m,k).eq.0.0)then

            kt=.false.

            goto 50

            end if

40        continue

50        dem_so_nt=kt

            End

9. VD 20: Ma trận chuyển vị

integer i,j

            integer array(2,3),c(3,2)

            print *,'Nhap ma tran A'

            do 10, i=1,2

            read *,(array(i,j),j=1,3)

10        continue

            c=transpose(array)

            print *

            print *,'Ma tran chuyen vi'

            do 20 i=1,3

            write (*,40)(c(i,j),j=1,2)

20        continue

40        format(2i)

            End

Bạn đang đọc truyện trên: Truyen2U.Pro