You are on page 1of 16

CF 1 LAB ASSIGNMENT 7

Task 1:
Write a program to print the following using DO loop.
1
123
12345
1234567
123456789

Codes for program:


PROGRAM PRINTING AN ARTHEMATIC SERIES
! PROGRAM STATEMENT
! Write a program to print the following using DO loop.
!1
!123
!12345
!1234567
!123456789
! =====================================

! DECLARATION STATEMENT
! =====================================
IMPLICIT NONE
INTEGER I, N, K
CHARACTER D
PRINT'(10X,A)','---------LAB 7 TASK 1----------'
PRINT'(10X,A)',' PRINTING AN ARTHEMATIC SERIES'
! INITILIZATION OF VARIABLES
! =====================================
N=9

920

FORMAT (/)

910

FORMAT (1X,I3,$)
! CALCULATIONS, LOGIC AND RESULT
! =====================================
DO I = 1, N, 2
DO K = 1, I
PRINT 910, K
END DO
PRINT 920,''
END DO

END PROGRAM PRINTING AN ARTHEMATIC SERIES

Result:

Task 2:
Write a program for Counting Digits and displaying their total count at the end of
the program.

Codes for program:


PROGRAM PRINTING AN ARTHEMATIC SERIES

! PROGRAM STATEMENT
! Write a program for Counting Digits in a given number
! and displaying their total count at the end of the program.
! =====================================

! DECLARATION STATEMENT
! =====================================
IMPLICIT NONE
INTEGER I, J, K, N, DIGIT
CHARACTER D
PRINT 910 ,'---------LAB 7 TASK 2----------'
PRINT'(2X,A)',' '

! INPUT
! =====================================
10

PRINT 920 , 'ENTER THE NUMBER'


READ*,N

! INITILIZATION OF VARIABLES & FORMATS


! =====================================
910

FORMAT (10X,A)

920

FORMAT (2X,A)

930

FORMAT (1X,A,I3,2X,A)
DIGIT = 1

! CALCULATIONS, LOGIC AND RESULT


! =====================================
DO I = 1,10
J = 10**I
K = MOD(N, J)

IF (K. LT. N) THEN


DIGIT = DIGIT + 1
END IF
END DO
PRINT 930,'YOU HAVE ENTERED A ',DIGIT,' DIGIT NUMBER'

PRINT 920 ,'DO YOU WANT TO CONTINUE (Y / N)'


READ*,D
IF (D. EQ. 'Y') GOTO 10
END PROGRAM PRINTING AN ARTHEMATIC SERIES

Result:

Task 3:
Implement the Euclidean Algorithm in FORTRAN, for finding the Greatest Common
Divisor (GCD) of two given positive integers.

Codes for program:


PROGRAM EUCLIDEAN ALGORITHIM
! PROGRAM STATEMENT
!\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

! Implement the Euclidean Algorithm in FORTRAN,


!for finding the Greatest Common Divisor (GCD)
!of two given positive integers.
!The algorithm transforms a pair of positive integers (m, n)
!into a pair (d, 0) by repeatedly dividing the larger integer
!by the smaller integer and replacing the larger with the
!remainder. When the remainder is zero,the other
!integer in the pair will be the GCD of the original pair.
!\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
! =====================================

! DECLARATION STATEMENT
! =====================================
IMPLICIT NONE
INTEGER M, N, R, GCD
CHARACTER D
PRINT 910 ,'---------LAB 7 TASK 3----------'
PRINT 920 ,'EUCLIDEAN ALGORITHIM (GREATEST COMMON DIVISOR '

! INPUT
! =====================================
10

PRINT 920 , 'ENTER THE GREATER NUMBER'


READ*,M
PRINT 920 , 'ENTER THE SMALLAR NUMBER'
READ*,N

! INITILIZATION OF VARIABLES & FORMATS


! =====================================
910

FORMAT (10X,A,/)

920

FORMAT (2X,A)

930

FORMAT (2X,A,I5)

! CALCULATIONS, LOGIC AND RESULT


! =====================================
20

R = MOD (M,N)
IF (R. GT. 0) THEN
M=N
N=R
ELSE IF (R. EQ. 0) THEN
GCD = N
END IF
IF (R. GT. 0) GOTO 20
PRINT 930 ,'THE GREATEST COMMON DIVISOR OF GIVEN NUMBERS IS ',GCD
PRINT 920 ,'DO YOU WANT TO CONTINUE (Y / N)'
READ*,D
IF (D. EQ. 'Y') GOTO 10
END PROGRAM EUCLIDEAN ALGORITHIM

Result:

Task 4:
Ahmad wants to input two numbers A and B, combine the two numbers in
another number C that is represented by the join of A and B and print the number,
like the example If A=12 and B=345 then C=12.345

Codes for program:


PROGRAM COMBINATION OF 2 NUMBERS
! PROGRAM STATEMENT
!\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
!Ahmad wants to input two numbers A and B,
!combine the two numbers in another number C
!that is represented by the join of A and B and print the number,
!like if A=12 and B=345 then C=12.345
!\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
! =====================================

! DECLARATION STATEMENT
! =====================================

IMPLICIT NONE
INTEGER I, J, K, DIGIT
REAL A, B, C
CHARACTER D
PRINT 910 ,'---------LAB 7 TASK 4----------'
PRINT'(2X,A)',' '
PRINT 910 ,'COMBINATION OF 2 NUMBERS A & B'
! INPUT
! =====================================
10

PRINT 920 , 'ENTER THE VALUE OF A'


READ*,A
PRINT 920 , 'ENTER THE VALUE OF B'
READ*,B

! INITILIZATION OF VARIABLES & FORMATS


! =====================================
910

FORMAT (10X,A)

920

FORMAT (2X,A)

930

FORMAT (2X,A,F8.3,/,/,5X,A,F8.3,/,/,7X,A,F8.3)
DIGIT = 1
! CALCULATIONS, LOGIC AND RESULT
! =====================================

20

DO I = 1,10
J = 10**I
K = MOD(B, J)
IF (K. LT. B) THEN
DIGIT = DIGIT + 1
END IF
END DO
C = A + (B / (10**DIGIT))

PRINT 930 ,' FOR A =',A,'& B =',B,'C =',C


PRINT 920 ,'DO YOU WANT TO CONTINUE (Y / N)'
READ*,D
IF (D. EQ. 'Y') GOTO 10
END PROGRAM COMBINATION OF 2 NUMBERS

Result:

Task 5:
Write a program such that a number C is given by user and break its decimal and
fraction part in A and B like shown: If C=12.345 then A=12 and B=345

Codes for program:


PROGRAM DECIMAL AND FRACTIONAL PART
! PROGRAM STATEMENT
!\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
!Ahmad wants to make the program such that a number C
!is given by user and break its decimal and fraction part in A
!and B like If C=12.345 then A=12 and B=345
!\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
! =====================================

! DECLARATION STATEMENT
! =====================================
IMPLICIT NONE
INTEGER A
REAL B, C
CHARACTER D
PRINT 910 ,'---------LAB 7 TASK 5----------'
PRINT'(2X,A)',' '
PRINT 910 ,'DECIMAL AND FRACTIONAL PART'
! INPUT
! =====================================
10

PRINT 920 , 'ENTER THE NUMBER C'


READ*,C
! INITILIZATION OF VARIABLES & FORMATS
! =====================================

910

FORMAT (10X,A)

920

FORMAT (/,2X,A)

930

FORMAT (2X,A,/,/,5X,I5,/,/,2X,A,/,/,5X,F10.5)
! CALCULATIONS, LOGIC AND RESULT
! =====================================

20

A=C
B=C-A

PRINT 930 ,'THE INTERGER PART OF NUMBER IS A =',A,' WHILE THE


FRACTIONAL
2 PART IS B =',B
PRINT 920 ,'DO YOU WANT TO CONTINUE (Y / N)'
READ*,D
IF (D. EQ. 'Y') GOTO 10
END PROGRAM DECIMAL AND FRACTIONAL PART

Result:

Task 6:
Take the values of x and y in XY coordinate system. The range of values of x are -1.5
to 2 with a step of 0.1 while the range of values of y are -1 to 1.5 with a step of 0.1.
Find how many points of this rectangular area lie within the circle having a radius of
1 and center at origin? (for each value of x check all values of y). Also find the
number of points in quadrant 1, 2, 3 and 4?

Codes for program:


PROGRAM POINTS IN A GIVEN CIRCLE OF RADIUS 1
! PROGRAM STATEMENT
!\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
!Take the values of x and y in XY coordinate system.
!The range of values of x are -1.5 to 2 with a step of 0.1 while
!the range of values of y are -1 to 1.5 with a step of 0.1.
!Find how many points of this rectangular area lie within the circle
!having a radius of 1 and centre at origin? (for each value of x check
!all values of y).
!Also find the number of points in quadrant 1,2, 3 and 4?
!\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
! =====================================

! DECLARATION STATEMENT
! =====================================
IMPLICIT NONE
REAL X, Y, A
INTEGER POINT, I, II, III, IV
! 'POINT' IS THE NUMBER OF POINTS IN CIRCLE, AND I,II,III,IV ARE
QUADRANTS
CHARACTER D
PRINT 910 ,'---------LAB 7 TASK 7----------'
PRINT 910 ,'POINTS IN A GIVEN CIRCLE OF RADIUS 1'

! INITILIZATION OF VARIABLES & FORMATS


! =====================================
910

FORMAT (10X,A,/)

920

FORMAT (/,2X,A)

930

FORMAT (2X,A,I3,/)
POINT = 0
I=0
II = 0
III = 0
IV = 0
! CALCULATIONS, LOGIC AND RESULT
! =====================================
DO X = -1.5,2.0,0.1
DO Y = -1,1.5,0.1
A = (X*X)+(Y*Y)
IF (A. LT. 1.0) THEN
POINT = POINT + 1
IF (X. GT. 0.0) THEN
IF (Y. GT. 0.0) THEN

I = I+1
ELSE
IV = IV + 1
END IF
END IF
IF (X. LT. 0.0) THEN
IF (Y. GT. 0.0) THEN
II = II+1
ELSE
III = III + 1
END IF
END IF
END IF
END DO
END DO
PRINT 930 ,'TOTAL POINTS IN THE CIRCLE = ',POINT
PRINT 930 ,'POINT IN 1ST QUADRANT = ',I
PRINT 930 ,'POINT IN 2ND QUADRANT = ',II
PRINT 930 ,'POINT IN 3RD QUADRANT = ',III
PRINT 930 ,'POINT IN 4TH QUADRANT = ',IV
END PROGRAM POINTS IN A GIVEN CIRCLE OF RADIUS 1

Result:

Task 7:
Take numbers from -2 to 2 with a step size of 0.1. Display the result of the values
according to the following mathematical function
If 0 x -2 then print x
If 1 x > 0 then print x2
If 2 x > 1 then print 1

Codes for program:


PROGRAM PRINTING A FUNCTION
! PROGRAM STATEMENT
!\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
!Take numbers from -2 to 2 with a step size of 0.1. Display the result of the
!values according to the following mathematical function
!If 0 x -2 then print x
!If 1 x > 0 then print x2
!If 2 x > 1 then print 1
!\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
! =====================================

! DECLARATION STATEMENT
! =====================================
IMPLICIT NONE
REAL X, Y
CHARACTER D
PRINT 910 ,'---------LAB 7 TASK 7----------'
PRINT 910 ,'PRINTING A FUNCTION'

! INITILIZATION OF VARIABLES & FORMATS


! =====================================
910

FORMAT (10X,A,/)

920

FORMAT (/,2X,A)

930

FORMAT (2X,F7.1,3X,F7.2)

940

FORMAT (7X,A,7X,A)

! CALCULATIONS, LOGIC AND RESULT


! =====================================
PRINT 940 ,'X','F(X)'
DO X = -2.0,2.0,0.1
IF (X. LE. 0.0) THEN
Y = -X
ELSE IF (X. LE. 1.0) THEN
Y = X**2
ELSE
Y = 1.0
END IF
PRINT 930 ,X,Y
END DO
END PROGRAM PRINTING A FUNCTION

Result:

You might also like