POKHARA UNIVERSITY
Faculty of Science and Technology
School of Engineering
Lab Report: FORTRAN Programming
Introduction to FORTRAN Programming: Looping Constructs, Mathematical
Functions, and Conditional Logic
Submitted By: Submitted To:
Suraj Adhikari Er. Paras Devkota
Roll: 41
17th Dec 2024
Objective: To gain familiarity with basic FORTRAN programming concepts,
focusing on looping constructs, mathematical functions, and conditional logic.
Additionally, to practice writing pseudocode, translating it into FORTRAN, and
executing programs effectively.
Task 1: Write a program to display the first 10 natural numbers.
Pseudocode:
Code:
Execution Steps:
Compile the program with DisplayNaturalNumber as file name:
Execute the compiled file:
Output:
Observations:
• Every Fortran program starts with the PROGRAM keyword and ends with
the END PROGRAM keyword.
• The IMPLICIT NONE statement prevents Fortran's default behavior of
automatically assigning data types to variables, ensuring that all variables are
explicitly declared.
• Variables are declared using appropriate data type keywords, such as
INTEGER, REAL, etc.
• The DO loop is a fundamental control structure in Fortran, used to iterate
through a range of numbers efficiently.
Task 2: Write a program to calculate and display the squares of the first 10
natural numbers.
Pseudocode:
Code:
Execution:
Compile the program SquareOfNaturalNum.f90 with gfortran and save file as
SquareOfNaturalNum and execute the program
Output:
Task 3: Calculate and display the cube root of the first 20 natural numbers.
Pseudocode:
Code:
Execution:
Compile the program CubeRootOfNaturalNum.f90 with gfortran and save file as
CubeRootOfNaturalNum and execute the program
Output:
Observations:
• Variables must be declared with appropriate data types, such as INTEGER,
REAL, etc., for type safety.
• The DO loop is used to iterate through a range of numbers and is compatible
only with integer variables.
• Fortran's default output spacing with PRINT* can be refined using formatted
WRITE statements for cleaner results.
Task 4: Calculate the sum and average of the first 10 odd natural numbers.
Pseudocode:
Code:
Execution:
Compile the program OddNaturalNum.f90 with gfortran and save file as
OddNaturalNum and execute the program
Output:
Observations:
• All variables like sum must be initialized first to avoid garbage value
• In DO loop, we can add a step parameter to iterate by certain value at a time
Task 5: Write a program to calculate the factorial of numbers from 1 to 5.
Pseudocode:
Code:
Execution:
Compile the program Factorial.f90 with gfortran and save file as Factorial and
execute the program
Output:
Task 6: Generate the Fibonacci sequence up to the 10th term.
Pseudocode:
Code:
Execution:
Compile the program Fibonacchi.f90 with gfortran and save file as Fibonacci and
execute the program
Output:
Task 7: Calculate the sum of the digits of a number entered by the user.
Pseudocode:
Code:
Execution:
Compile the program SumOfDigits.f90 with gfortran and save file as
SumOfDigits. Execute the program then enter a number for which sum of digits
needs to be calculated as input in terminal
Output:
Observations:
• The WRITE statement with ADVANCE="NO" allows input prompts to
stay on the same line for better user experience.
• The READ statement takes user input for the variable n.
• A DO WHILE loop processes the number, extracting and summing its digits
using the MOD function.
Task 8: Check if a number entered by the user is prime.
Pseudocode:
Code:
Execution:
Compile the program CheckPrimeNum.f90 with gfortran and save file as
CheckPrimeNum. Execute the program then enter a number for which sum of
digits needs to be calculated as input in terminal
Output:
Observations:
• The REAL and SQRT functions are used to calculate the square root of the number,
converted to an integer with INT to determine the iteration limit for efficiency.
• The EXIT statement breaks the loop as soon as a divisor is found, improving performance
for non-prime numbers.
• A flag (flag) is used to track whether a divisor was found, simplifying the logic for the final
decision.
Conclusion:
The lab session provided hands-on experience with FORTRAN programming. We gained
familiarity with basic constructs such as loops, arithmetic operations, and conditional logic
while exploring practical applications.