Assignment 1 Code Input Output
Assignment 1 Code Input Output
Code: mat.f90
program matrix
implicit none
integer::A(4,4),i,j,d3=0,d2
open(unit=393,file='mat_in.txt')
open(unit=939,file='mat_out.txt')
read(393,*)((A(i,j),j=1,4),i=1,4)
write(939,'(//,a,/)')"(ii) horizontal"
write(939,'(i7,15i3)')((A(i,j),j=1,4),i=1,4)
write(939,'(//,a,/)')"(iii) vertical"
write(939,'(i7)')((A(i,j),j=1,4),i=1,4)
do i=1,4
do j=1,4
if (i==j)d3= d3 + A(i,j)**3
end do
end do
do i=1,4
d2=d2+A(i,5-i)**2
end do
write(939,'(a,i0,/,4x,a,i0)')"(v) d3=",d3,"d2=",d2
if (d3>d2) write(939,'(a)')"therefore d3>d2"
if (d3<d2) write(939,'(a)')"therefore d3<d2"
if (d3==d2) write(939,'(a)')"therefore d3=d2"
end program
Input: mat_in.txt
1 7 3 4
5 2 6 0
0 9 3 5
8 1 7 6
Output: mat_out.txt
1 7 3 4
5 2 6 0
0 9 3 5
8 1 7 6
(ii) horizontal
1 7 3 4 5 2 6 0 0 9 3 5 8 1 7 6
(iii) vertical
1
7
3
4
5
2
6
0
0
9
3
5
8
1
7
6
1 7 3 4
5
0
8
(v) d3=252
d2=197
therefore d3>d2
1 5 0 8
7 2 9 1
3 6 3 7
4 0 5 6
Code: as1_2.f90
program array
implicit none
integer::n,i,j,temp
integer,allocatable::arr(:)
open(unit=30,file='array_in.txt')
open(unit=32,file='array_out.txt')
read(*,*)n
if(n<5) then
write(*,*)"n must be at least 5"
stop
end if
allocate(arr(n))
do i=1,n
read(30,*)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(32,'("ascending order:")')
do i=1,n
write(32,*)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(32,'("descending order:")')
do i=1,n
write(32,*)arr(i)
end do
end program
Input: array_in.txt
1
1
2
3
5
8
5
34
2
4
6
8
56
42
Output: array_out.txt
ascending order:
1
1
2
3
5
5
8
34
descending order:
34
8
5
5
3
2
1
1
Code: as1_3.f90
program descriptor
implicit none
integer::i,j
real::a(6,6)
do i=1,6
do j=1,6
call random_number(a(i,j))
a(i,j)=10.+a(i,j)*10.
end do
end do
open(unit=377,file='Fdes_out.txt')
write(377,'(a,/)')"F descriptor"
write(377,'(6f20.8)')((a(i,j),j=1,6),i=1,6)
write(377,'(///,a,/)')"E descriptor"
write(377,'(6e20.7)')((a(i,j),j=1,6),i=1,6)
write(377,'(///,a,/)')"ES descriptor"
write(377,'(6es20.7)')((a(i,j),j=1,6),i=1,6)
close(377)
end program
Input: Fdes_in.txt
Output: Fdes_out.txt
F descriptor
E descriptor
ES descriptor
Code: as1_4.f90
program gcd
implicit none
integer::a,b,ans
open(unit=42,file='gcd_in.txt')
open(unit=62,file='gcd_out.txt')
read(42,*)a,b
ans=g(a,b)
write(62,*)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
5
10
Output: gcd_out.txt
Code: as1_5.f90
program Prime_Num
implicit none
integer::n,f
logical::p
open(unit=67,file='fermat_out.txt')
do n=1,4
f=(2**(2**n))+1
p=prime(f)
write(67,'(i20,5x,l)')f,p
end do
contains
function prime(x) result(p)
integer::x,i
logical::p
do i=2,x/2
if(mod(x,i)==0)then
p=.false.
exit
else
p=.true.
end if
end do
end function
end program
Output: fermat_out.txt
5 T
17 T
257 T
65537 T
Code: as1_6.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)
!write(*,*)((a(i,j),j=1,c1),i=1,r1)
!write(*,*)((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
19.0000 22.0000
43.0000 50.0000
T
Code: as1_7.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