0% found this document useful (0 votes)
3 views9 pages

Assignment_1_Code_Input_Output_A

The document contains solutions to seven programming assignments in Fortran, each addressing different computational problems. Assignments include matrix operations, sorting algorithms, random number generation, GCD calculation, prime number testing, matrix multiplication, and finding amicable numbers. Each assignment includes code snippets, input/output examples, and the results of the computations.

Uploaded by

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

Assignment_1_Code_Input_Output_A

The document contains solutions to seven programming assignments in Fortran, each addressing different computational problems. Assignments include matrix operations, sorting algorithms, random number generation, GCD calculation, prime number testing, matrix multiplication, and finding amicable numbers. Each assignment includes code snippets, input/output examples, and the results of the computations.

Uploaded by

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

Assignment-1

Solved by Md Ashik Ali (AE-123-082)

Answer to the question no. 1


Code: A1Q1.f90
program apple
implicit none
integer::i,j,d3,d2
integer,Dimension(4,4)::A

open(unit=44,file='matins.dat')
open(unit=55,file='outmat.txt')

read(44,*)((A(i,j),j=1,4),i=1,4)

write(55,*)"Answer of No (i)"
write(55,'(4I3)')((A(i,j),j=1,4),i=1,4)

write(55,'(/a)')"Answer of No (ii)"
write(55,'(16I3)')((A(i,j),j=1,4),i=1,4)

write(55,'(/a)')"Answer of No (iii)"
write(55,'(I3)')((A(i,j),j=1,4),i=1,4)

write(55,'(/a)')"Answer of No (iv)"
write(55,'(4I3)')(A(1,j),j=1,4)
write(55,'(I3)')(A(i,1),i=2,4)

write(55,'(/a)')"Answer of No (v)"

do i=1,4
do j=1,4

d3=0;
if(i==j)then
d3=d3+A(i,j)**3
end if
end do
end do

d2=0;
do i=1,4
d2=d2+A(i,5-i)
end do

write(55,*)"D3= ",d3
write(55,*)"D2= ",d2
If(d3>d2)write(55,*)"D3>D2"

write(55,'(/a)')"Answer of No (vi)"
write(55,'(4I3)')((A(i,j),i=1,4),j=1,4)
close(44)
close(55)
end

Input: matins.dat
1 7 3 4
5 2 6 0
0 9 3 5
8 1 7 6

Output: outmat.txt
Answer of No (i)
1 7 3 4
5 2 6 0
0 9 3 5
8 1 7 6

Answer of No (ii)
1 7 3 4 5 2 6 0 0 9 3 5 8 1 7 6

Answer of No (iii)
1
7
3
4
5
2
6
0
0
9
3
5
8
1
7
6

Answer of No (iv)
1 7 3 4
5
0
8

Answer of No (v)
D3= 216
D2= 27
D3>D2

Answer of No (vi)
1 5 0 8
7 2 9 1
3 6 3 7
4 0 5 6

Answer to the question no. 2


Code: A1Q2.f90
program array
implicit none
integer::i,j,temp,n
integer,allocatable::arr(:)
open(unit=2,file='arrayout.txt')

write(*,*)"Write the value of n: "


read(*,*)n
if(n<5)then
write(*,*)"Error! Obviously n must at least 5."
stop
end if

allocate(arr(n))

do i=1,n
read(*,*)arr(i)
end do

do i=1,n-1
do j=1,n-1
if(arr(j)>arr(j+1))then
temp=arr(j)
arr(j)=arr(j+1)
arr(j+1)=temp
end if
end do
end do

write(2,*)"Ascendending Order: "


do i=1,n
write(2,*)arr(i)
end do

write(2,*)"Discendending Order: "


do i=n,1,-1
write(2,*)arr(i)
end do

end program
Output: arrayout.txt
Ascendending Order:
23
34
43
64
68
Discendending Order:
68
64
43
34
23

Answer to the question no. 3


Code: A1Q3.f90
program descriptor
implicit none
integer::i,j
real,dimension(6,6)::a

open(unit=3,file='desout.txt')

do i=1,6
do j=1,6
call random_number (a(i,j))
a(i,j)=10+10*a(i,j)
end do
end do

open(unit=55,file='desoutput')
write(3,'(a/)')"F Descriptor."
write(3,'(6f20.8)')((a(i,j),j=1,6),i=1,6)

write(3,'(a/)')"E Descriptor."
write(3,'(6e20.7)')((a(i,j),j=1,6),i=1,6)

write(3,'(a/)')"Es Descriptor."
write(3,'(6es20.7)')((a(i,j),j=1,6),i=1,6)

end

Output: desout.txt
F Descriptor.

19.97559547 15.66824722 19.65915298 17.47927666


13.67390823 14.80636883
10.73754215 10.05355167 13.47081280 13.42243767
12.17951679 11.33160400
19.00524521 13.86765957 14.45482254 16.61932182
10.16108322 16.50854874
16.46408844 13.22987270 18.55692291 14.01286888
12.06874275 19.68539429
15.98399544 16.72980690 14.56882286 13.30015087
11.00382900 17.55453300
16.05693245 17.19047928 18.97334671 16.58229065
11.50716782 16.12314796
E Descriptor.

0.1997560E+02 0.1566825E+02 0.1965915E+02 0.1747928E+02


0.1367391E+02 0.1480637E+02
0.1073754E+02 0.1005355E+02 0.1347081E+02 0.1342244E+02
0.1217952E+02 0.1133160E+02
0.1900525E+02 0.1386766E+02 0.1445482E+02 0.1661932E+02
0.1016108E+02 0.1650855E+02
0.1646409E+02 0.1322987E+02 0.1855692E+02 0.1401287E+02
0.1206874E+02 0.1968539E+02
0.1598400E+02 0.1672981E+02 0.1456882E+02 0.1330015E+02
0.1100383E+02 0.1755453E+02
0.1605693E+02 0.1719048E+02 0.1897335E+02 0.1658229E+02
0.1150717E+02 0.1612315E+02
Es Descriptor.

1.9975595E+01 1.5668247E+01 1.9659153E+01 1.7479277E+01


1.3673908E+01 1.4806369E+01
1.0737542E+01 1.0053552E+01 1.3470813E+01 1.3422438E+01
1.2179517E+01 1.1331604E+01
1.9005245E+01 1.3867660E+01 1.4454823E+01 1.6619322E+01
1.0161083E+01 1.6508549E+01
1.6464088E+01 1.3229873E+01 1.8556923E+01 1.4012869E+01
1.2068743E+01 1.9685394E+01
1.5983995E+01 1.6729807E+01 1.4568823E+01 1.3300151E+01
1.1003829E+01 1.7554533E+01
1.6056932E+01 1.7190479E+01 1.8973347E+01 1.6582291E+01
1.1507168E+01 1.6123148E+01

Answer to the question no. 4


Code: A1Q4.f90
program gcd
implicit none
integer::a,b,ans
open(unit=4,file='gcd_in.txt')
open(unit=6,file='gcd_out.txt')

read(4,*)a,b
ans=g(a,b)
write(6,*)ans

contains
recursive function g(x,y) result(gcd)
integer::x,y,gcd

if(mod(x,y)==0)then
gcd=y
else
gcd=g(y,mod(x,y))
end if
end function
end program

Input: gcd_in.txt
40
60

Output: gcd_out.txt
20

Answer to the question no. 5


Code: A1Q5.f90
program prime_number
implicit none
integer::i,n,f,prime
logical::p

open(unit=34,file='PrimeOutput.txt')
write(34,'(t12,"n",15x,"Fn",6x,"Prime Test")')
write(34,'(t8,40("-"))')
do n=1,4
f=2**(2**n)+1
p=prime(f)
write(34,'(t11,i2,12x,i5,10x,l)')n,f,p
end do

end program

function prime(n)
implicit none
integer::i,n,prime
logical::p
do i=1,n/2
if(mod(n,i)==0)then
p=.false.
else
p=.true.
end if
end do
end function

Output: PrimeOutput.txt
n Fn Prime Test
----------------------------------------
1 5 T
2 17 T
3 257 T
4 65537 T
Answer to the question no. 6
Code: A1Q6.f90
program matmultp
implicit none
integer::r1,c1,r2,c2,i,j,k
real,allocatable::a(:,:),b(:,:),c(:,:)
logical::v

read(*,*)r1,c1,r2,c2
open(unit=987,file='matmult_in.txt')
open(unit=977,file='matmult_out.txt')
allocate(a(1:r1, 1:c1), b(1:r2, 1:c2), c(1:r1, 1:c2))
read(987,*)((a(i,j),j=1,c1),i=1,r1)
read(987,*)((b(i,j),j=1,c2),i=1,r2)

call mat(a,r1,c1,b,r2,c2,c)
write(977,'(2(f15.4,2x))')((c(i,j),j=1,c2),i=1,r1)

v=g(a,r1,c1,b,r2,c2,c)
write(977,*)v

contains
function g(a,r1,c1,b,r2,c2,c) result(v)
implicit none
integer::i,j,r1,r2,c1,c2
real::a(1:r1,1:c1),b(1:r2,1:c2),c(1:r1, 1:c2),d(1:r1, 1:c2)
logical::v

d=matmul(a,b)
v=.true.
do i=1,r1
do j=1,c2
if(c(i,j)/=d(i,j))then
v=.false.
exit
end if
end do
end do
end function

end program
subroutine mat(a,r1,c1,b,r2,c2,c)
integer::r1,c1,r2,c2
real::a(1:r1,1:c1),b(1:r2,1:c2),c(1:r1, 1:c2)
c=0
do i=1,r1
do j=1,c2
do k=1,c1
c(i,j)=c(i,j)+a(i,k)*b(k,j)
end do
end do

end do
end subroutine

Input: matmult_in.txt
1. 2. 3. 4. 4. 6. 7.
5. 6. 7. 8. 5. 8. 3.
9. 10. 11. 12. 1. 4. 6.
13. 14. 15. 16. 5. 5. 7.
1. 5. 6. 7. 8. 5. 5.
3. 4. 5. 8. 5. 4. 2.

Output: matmult_out.txt
34.0000 46.0000
78.0000 104.0000
T

Answer to the question no. 7


Code: A1Q7.f90
program Calc_Amicable_Num
implicit none
integer, parameter :: N=67000
integer :: i, calc_divisor_sum, divisorSum1, divisorSum2, prev
open(unit=78,file='amicable_out.txt')

do i=1, N
divisorSum1 = calc_divisor_sum(i)
divisorSum2 = calc_divisor_sum(divisorSum1)
if((divisorSum2 == i ).and. (i .ne. divisorSum1)) then
if(prev .ne. divisorSum1) then
write(78,*) i, divisorSum1
end if
prev = i
end if
end do
end program

function calc_divisor_sum(n)
implicit none
integer :: i, n, res, calc_divisor_sum
res=0
do i=1, nint(sqrt(real(n)))
if(mod(n, i) == 0) then
res = res + i
! As we are considering proper divisors
if(i == 1) then
cycle
end if
res = res + (n/i)
end if
end do
calc_divisor_sum = res
end function

Output: amicable_out.txt
220 284
1184 1210
2620 2924
5020 5564
6232 6368
10744 10856
12285 14595
17296 18416
63020 76084
66928 66992

You might also like