How to Modify a FORTRAN Program to Exclude Multiples of 5?

  • Context: Fortran 
  • Thread starter Thread starter Nicolaus
  • Start date Start date
  • Tags Tags
    Fortran
Click For Summary
SUMMARY

The discussion focuses on modifying a FORTRAN program to exclude integers that are multiples of 5 from its output. The original program prompts the user for a range of integers and prints their square roots. To achieve the desired functionality, an if statement utilizing the modulus operator (%) is recommended to check if an integer is a multiple of 5. Specifically, the condition "if (i % 5 .NE. 0)" should be added before the output statement to filter out these values.

PREREQUISITES
  • Basic understanding of FORTRAN programming
  • Familiarity with control structures in programming (if statements)
  • Knowledge of the modulus operator (%) and its application
  • Experience with input/output operations in FORTRAN
NEXT STEPS
  • Implement the suggested if statement in the FORTRAN program to exclude multiples of 5
  • Explore additional control structures in FORTRAN for more complex filtering
  • Learn about error handling in FORTRAN to improve user input validation
  • Investigate performance considerations when processing large ranges in FORTRAN
USEFUL FOR

This discussion is beneficial for FORTRAN developers, students learning programming concepts, and anyone interested in modifying existing code for specific output requirements.

Nicolaus
Messages
73
Reaction score
0
How would I modify the program so it doesn't generate an output for integers that are multiples of 5? (e.g. if range was 1-20, then no lines would be printed for 5, 10, 15, 20.)

program squares
implicit none

integer*2 start, finish, i

! ----------------------------------------------Prompt and Input
print*, "Enter the range (two integers) ..."
read*, start, finish

! ----------------------------------------------Validation & Processing
if (start .GT. finish) then
print*, "Invalid: the start must be less than the end value!"
else if (start .LT. 0) then
print*, "Invalid: the range must not have negative values!"
else
do i = start, finish
write(*,*) i, sqrt(i*1.)
end do
end if
! ----------------------------------------------
end
 
Technology news on Phys.org


You could use an if statement that checks whether i % 5 == 0. The % operator is modulus and returns the remainder after division.
 

Similar threads

  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 20 ·
Replies
20
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 19 ·
Replies
19
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 13 ·
Replies
13
Views
3K