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

  • Fortran
  • Thread starter Nicolaus
  • Start date
  • Tags
    Fortran
If the remainder is 0, then the number is a multiple of 5 and you can skip printing it. In summary, to modify the program so it doesn't generate output for integers that are multiples of 5, you can use an if statement to check for the remainder and skip printing those numbers.
  • #1
Nicolaus
73
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
  • #2


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

1. What is a do-loop in FORTRAN?

A do-loop in FORTRAN is a control structure that allows a set of statements to be executed repeatedly until a specified condition is met. It is commonly used for iterating through arrays or performing a certain task a specific number of times.

2. How do I write a do-loop in FORTRAN?

A do-loop in FORTRAN is written using the DO keyword, followed by a loop control variable, a starting value, an end value, and an optional increment value. The loop body is then enclosed in a set of statements between the DO and END DO keywords.

3. Why am I getting an infinite loop in my FORTRAN do-loop?

An infinite loop in a FORTRAN do-loop can occur if the loop control variable is not being updated properly. This can lead to the loop condition always being true, causing the loop to continue indefinitely. Make sure to properly update the loop control variable within the loop body.

4. Can I use a do-loop to iterate through a multi-dimensional array in FORTRAN?

Yes, a FORTRAN do-loop can be used to iterate through multi-dimensional arrays. However, you will need to use nested do-loops to iterate through each dimension of the array.

5. How do I terminate a do-loop early in FORTRAN?

To terminate a do-loop early in FORTRAN, you can use the EXIT or CYCLE keywords. EXIT will completely terminate the loop, while CYCLE will skip over any remaining statements in the loop and begin the next iteration.

Similar threads

  • Programming and Computer Science
Replies
4
Views
589
  • Programming and Computer Science
Replies
12
Views
953
  • Programming and Computer Science
Replies
22
Views
730
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
29
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
12
Views
2K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
13
Views
2K
Back
Top